-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvr.lua
More file actions
518 lines (454 loc) · 16.1 KB
/
vr.lua
File metadata and controls
518 lines (454 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
--[[
Note that this is not a final product, many new features will be added as I develop this script!
- infiniteraymond
]]
local p = owner
local rss = game:GetService("ReplicatedStorage")
for _,v in pairs(game.ReplicatedStorage:GetChildren()) do
if v.Name == "Remotes" then
v.Name = "nilllll"
end
end
for _, v in pairs(workspace:GetChildren()) do
if v.Name == p.Name .. "_VR_" then
v:Destroy()
end
end
local character = p.Character
local vr = {}
local models = {
rig = Instance.new("Model")
}
function tween(t,g)
game:GetService("TweenService"):Create(t,TweenInfo.new(0.5,Enum.EasingStyle.Cubic),g):Play()
end
local remotes = {
initiate = Instance.new("RemoteEvent");
update = Instance.new("RemoteEvent");
summon = Instance.new("RemoteEvent");
}
local lhpL = nil
local rhpL = nil
local usedAlreadyL = false
local usedAlreadyR = false
local cooldownL = 0
local cooldownR = 0
local folder = Instance.new("Folder")
folder.Parent = rss
folder.Name = "Remotes"
remotes.initiate.Parent = folder
remotes.update.Parent = folder
remotes.summon.Parent = folder
remotes.initiate.Name = "VRInit"
remotes.update.Name = "VRUpd"
remotes.summon.Name = "VRSum"
vr.initiate = function()
print("VR IS WORKING")
local characterRig = models.rig
characterRig.Parent = workspace:WaitForChild(p.Name)
characterRig.Name = p.Name .. "_VR_"
local h = character.Head:Clone()
h.Parent = models.rig
h.Anchored = true
local la = character["Left Arm"]:Clone()
la.Parent = models.rig
la.Anchored = true
la.Size = Vector3.new(0.5, 1.25, 0.5)
local ra = character["Right Arm"]:Clone()
ra.Parent = models.rig
ra.Anchored = true
ra.Size = Vector3.new(0.5, 1.25, 0.5)
local shirt = character.Shirt:Clone()
shirt.Parent = models.rig
local hum = character.Humanoid:Clone()
hum.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
hum.Parent = models.rig
for _, v in pairs(character:GetChildren()) do
if v:IsA("Accoutrement") then
print("ACCESSORY MODIFIED")
if v:WaitForChild("Handle"):FindFirstChild("HairAttachment") then
v:WaitForChild("Handle"):FindFirstChild("AccessoryWeld").Part1 = characterRig.Head
elseif v:WaitForChild("Handle"):FindFirstChild("HatAttachment") then
v:WaitForChild("Handle"):FindFirstChild("AccessoryWeld").Part1 = characterRig.Head
elseif v:WaitForChild("Handle"):FindFirstChild("FaceFrontAttachment") then
v:WaitForChild("Handle"):FindFirstChild("AccessoryWeld").Part1 = characterRig.Head
else
v:Destroy()
end
end
end
wait(1)
character.PrimaryPart.CFrame = CFrame.new(0,-20,0)
character.HumanoidRootPart.Anchored = true
end
vr.update = function(_, data)
local character = workspace:WaitForChild(p.Name):WaitForChild(p.Name .. "_VR_")
tween(character:WaitForChild("Head"),{CFrame = data.Head})
tween(character:WaitForChild("Left Arm"),{CFrame = data.Left})
tween(character:WaitForChild("Right Arm"),{CFrame = data.Right})
if data.GrabbingLeft then
local v = data.GrabbingLeft
if v:IsA("Part") or v:IsA("MeshPart") then
if lhpL == nil and (v.Position - character:WaitForChild("Left Arm").Position).magnitude <= 2.5 and not v.Anchored then
lhpL = v
print("held " .. v.Name:lower())
local weld = Instance.new("WeldConstraint")
weld.Parent = v
if v.Name == "Pistol" then
--v.CFrame = character:WaitForChild("Left Arm").CFrame * CFrame.new(0,0,0.75) * CFrame.Angles(math.rad(90),0,0)
end
weld.Part0 = character:WaitForChild("Left Arm")
weld.Part1 = v
weld.Name = "GrabWeld"
elseif v:FindFirstChild("VRInteractive") and lhpL == nil and (v.Position - character:WaitForChild("Left Arm").Position).magnitude <= 2.5 then
lhpL = v
local weld = Instance.new("WeldConstraint")
weld.Name = "InteractionWeld"
weld.Parent = v
print("interacted with ".. v.Name)
end
end
if data.TriggeringLeft then
if v.Name == "Pistol" and not usedAlreadyL then
usedAlreadyL = true
vr.use("left")
elseif v.Name == "M4A1" and (tick() - cooldownL) >= 0.1 then
cooldownL = tick()
vr.use("left")
end
end
end
if data.GrabbingRight then
local v = data.GrabbingRight
if v:IsA("Part") or v:IsA("MeshPart") then
if rhpL == nil and (v.Position - character:WaitForChild("Right Arm").Position).magnitude <= 2.5 and not v.Anchored then
rhpL = v
print("held " .. v.Name:lower())
local weld = Instance.new("WeldConstraint")
weld.Parent = v
if v.Name == "Pistol" then
--v.CFrame = character:WaitForChild("Right Arm").CFrame * CFrame.new(0,0,0.75) * CFrame.Angles(math.rad(90),0,0)
end
weld.Part0 = character:WaitForChild("Right Arm")
weld.Part1 = v
weld.Name = "GrabWeld"
elseif v:FindFirstChild("VRInteractive") and rhpL == nil and (v.Position - character:WaitForChild("Right Arm").Position).magnitude <= 2.5 then
rhpL = v
local weld = Instance.new("WeldConstraint")
weld.Name = "InteractionWeld"
weld.Parent = v
print("interacted with ".. v.Name)
end
end
if data.TriggeringRight then
if v.Name == "Pistol" and not usedAlreadyR then
usedAlreadyR = true
vr.use("right")
elseif v.Name == "M4A1" and (tick() - cooldownR) >= 0.1 then
cooldownR = tick()
vr.use("right")
end
end
end
if not data.TriggeringRight and usedAlreadyR then
usedAlreadyR = false
end
if not data.TriggeringLeft and usedAlreadyL then
usedAlreadyL = false
end
if lhpL or rhpL then
if lhpL and not data.GrabbingLeft then
if lhpL:FindFirstChild("GrabWeld") then
lhpL:FindFirstChild("GrabWeld"):Destroy()
local bv = Instance.new("BodyVelocity",lhpL)
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bv.Velocity = la.CFrame.lookVector*40
game.Debris:AddItem(bv,0.15)
end
if lhpL:FindFirstChild("InteractionWeld") then
lhpL:FindFirstChild("InteractionWeld"):Destroy()
local bv = Instance.new("BodyVelocity",lhpL)
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bv.Velocity = la.CFrame.lookVector*40
game.Debris:AddItem(bv,0.15)
end
lhpL = nil
print("let go of object on l")
end
if rhpL and not data.GrabbingRight then
if rhpL:FindFirstChild("GrabWeld") then
rhpL:FindFirstChild("GrabWeld"):Destroy()
local bv = Instance.new("BodyVelocity",rhpL)
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bv.Velocity = ra.CFrame.lookVector*40
game.Debris:AddItem(bv,0.15)
end
if rhpL:FindFirstChild("InteractionWeld") then
rhpL:FindFirstChild("InteractionWeld"):Destroy()
local bv = Instance.new("BodyVelocity",rhpL)
bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
bv.Velocity = ra.CFrame.lookVector*40
game.Debris:AddItem(bv,0.15)
end
rhpL = nil
print("let go of object on r")
end
end
-- Temporarily Removed as it breaks way too much
if lhpL and rhpL then
if (rhpL.Name == "Torso" or rhpL.Name == "HumanoidRootPart") then
if (lhpL.Name == "Head" or lhpL.Name == "Handle") then
if lhpL.Name == "Handle" and not lhpL:FindFirstChild("HatAttachment") then return end
if (lhpL.Position - rhpL.Position).magnitude >= 2 then
if lhpL.Name == "Handle" then
lhpL.Parent.Parent:FindFirstChild("Humanoid").Health = 0
else
lhpL.Parent:FindFirstChild("Humanoid").Health = 0
end
end
end
elseif (lhpL.Name == "Torso" or lhpL.Name == "HumanoidRootPart") then
if (rhpL.Name == "Head" or rhpL.Name == "Handle") then
if rhpL.Name == "Handle" and not rhpL:FindFirstChild("HatAttachment") then return end
if (rhpL.Position - lhpL.Position).magnitude >= 2 then
if rhpL.Name == "Handle" then
rhpL.Parent.Parent:FindFirstChild("Humanoid").Health = 0
else
rhpL.Parent:FindFirstChild("Humanoid").Health = 0
end
end
end
end
end
end
vr.summon = function(_, guntype)
local gun
if guntype == "Pistol" then
gun = Instance.new("Part")
gun.Name = "Pistol"
gun.CFrame = workspace:WaitForChild(p.Name):WaitForChild(p.Name .. "_VR_").Head.CFrame * CFrame.new(0,0,-5)
gun.CanCollide = true
gun.Size = Vector3.new(0.25, 0.7, 0.25)
local mesh = Instance.new("SpecialMesh")
mesh.Parent = gun
mesh.MeshId = "rbxassetid://1470036430"
mesh.TextureId = "rbxassetid://1470036477"
mesh.Offset = Vector3.new(0,0,-0.4)
mesh.Scale = Vector3.new(0.025,0.025,0.025)
gun.Parent = workspace
elseif guntype == "M4A1" then
gun = Instance.new("Part")
gun.Name = "M4A1"
gun.CFrame = workspace:WaitForChild(p.Name):WaitForChild(p.Name .. "_VR_").Head.CFrame * CFrame.new(0,0,-5)
gun.CanCollide = true
gun.Size = Vector3.new(0.25, 0.7, 0.25)
local mesh = Instance.new("SpecialMesh")
mesh.Parent = gun
mesh.MeshId = "rbxassetid://477116314"
mesh.TextureId = "rbxassetid://477116318"
mesh.Offset = Vector3.new(0,0,-0.7)
mesh.Scale = Vector3.new(0.025,0.025,0.025)
gun.Parent = workspace
end
local sound = Instance.new("Sound")
sound.Parent = gun
sound.Name = "GunShot"
sound.SoundId = "rbxassetid://6661889492"
end
vr.use = function(arm)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {workspace:WaitForChild(p.Name):WaitForChild(p.Name .. "_VR_"), lhpL, rhpL}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
if arm == "left" and lhpL then
if lhpL:FindFirstChild("GunShot") then
lhpL:FindFirstChild("GunShot"):Play()
end
local HitRay = workspace:Raycast(lhpL.Position, (lhpL.CFrame * CFrame.new(0,0,-1000)).p, raycastParams)
local hit
if HitRay then
hit = HitRay.Instance
end
if lhpL.Name == "M4A1" and hit then
print("left hit part: " .. hit.Name)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:FindFirstChild("Humanoid").Health -= 5
end
elseif lhpL.Name == "Pistol" and hit then
print("left hit part: " .. hit.Name)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:FindFirstChild("Humanoid").Health -= 25
end
end
end
if arm == "right" and rhpL then
if rhpL:FindFirstChild("GunShot") then
rhpL:FindFirstChild("GunShot"):Play()
end
local HitRay = workspace:Raycast(rhpL.Position, (rhpL.CFrame * CFrame.new(0,0,-1000)).p, raycastParams)
local hit
if HitRay then
hit = HitRay.Instance
end
if rhpL.Name == "M4A1" and hit then
print("right hit part: " .. hit.Name)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:FindFirstChild("Humanoid").Health -= 5
end
elseif rhpL.Name == "Pistol" and hit then
print("right hit part: " .. hit.Name)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent:FindFirstChild("Humanoid").Health -= 25
end
end
end
end
remotes.initiate.OnServerEvent:Connect(vr.initiate)
remotes.update.OnServerEvent:Connect(vr.update)
remotes.summon.OnServerEvent:Connect(vr.summon)
local check
check = game:GetService("RunService").Heartbeat:Connect(function()
if not game:GetService("Players"):FindFirstChild(p.Name) then
for _,v in pairs(game.ReplicatedStorage:GetChildren()) do
if v.Name == "Remotes" then
v.Name = "nilllll"
end
end
if workspace:FindFirstChild(p.Name .. "_VR_") then
workspace:FindFirstChild(p.Name .. "_VR_"):Destroy()
end
check:Disconnect()
end
end)
NLS([[
wait(2)
print("Starting VR (Client)...")
function tween(t,g)
game:GetService("TweenService"):Create(t,TweenInfo.new(0.5,Enum.EasingStyle.Cubic),g):Play()
end
local rss = game:GetService("ReplicatedStorage")
local rs = game:GetService("RunService")
local vr = game:GetService("VRService")
local uis = game:GetService("UserInputService")
local cam = workspace.CurrentCamera
local move = 0
local holdingRight = false
local holdingLeft = false
local trc = false
local tlc = false
local player = game.Players.LocalPlayer
local remotes = {
initiate = rss:WaitForChild("Remotes"):WaitForChild("VRInit");
update = rss:WaitForChild("Remotes"):WaitForChild("VRUpd");
summon = rss:WaitForChild("Remotes"):WaitForChild("VRSum");
}
local function gprtc(cf)
return cam.CFrame*CFrame.new(cf.p*cam.HeadScale) * (cf - cf.p)
end
if vr.VREnabled then
print("VR is enabled! Attempting to FireServer..")
remotes.initiate:FireServer()
local character = workspace:WaitForChild(player.Name):WaitForChild(player.Name .. "_VR_")
print("Successfully got ahold of server, and VR player has been created.")
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = player.Character.Head.CFrame
for _,v in pairs(player.Character.Humanoid:GetAccessories()) do
v.Handle.Transparency = 1
end
rs.Heartbeat:Connect(function()
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local GroundRay = workspace:Raycast(character:WaitForChild("Head").Position, character:WaitForChild("Head").Position - Vector3.new(0,1000,0), raycastParams)
local GrabRayR = workspace:Raycast(character:WaitForChild("Right Arm").Position, (character:WaitForChild("Right Arm").CFrame * CFrame.new(0,-250,0)).p - character:WaitForChild("Right Arm").Position, raycastParams)
local GrabRayL = workspace:Raycast(character:WaitForChild("Left Arm").Position, (character:WaitForChild("Left Arm").CFrame * CFrame.new(0,-250,0)).p - character:WaitForChild("Right Arm").Position, raycastParams)
if GroundRay then
if not GroundRay.Instance.Parent:FindFirstChild("Humanoid") and not GroundRay.Instance.Parent:IsA("Accoutrement") and not GroundRay.Instance:FindFirstChild("GrabWeld") then
cam.CFrame = CFrame.new(cam.CFrame.X, GroundRay.Position.Y + 5, cam.CFrame.Z)
end
end
local phl = false
local phr = false
if holdingRight and GrabRayR then
phr = GrabRayR.Instance
end
if holdingLeft and GrabRayL then
phl = GrabRayL.Instance
end
local data = {
Head = gprtc(vr:GetUserCFrame(Enum.UserCFrame.Head));
Right = gprtc(vr:GetUserCFrame(Enum.UserCFrame.RightHand)) * CFrame.Angles(90,0,0);
Left = gprtc(vr:GetUserCFrame(Enum.UserCFrame.LeftHand)) * CFrame.Angles(90,0,0);
GrabbingRight = phr;
GrabbingLeft = phl;
TriggeringRight = trc;
TriggeringLeft = tlc;
}
character:WaitForChild("Head").CFrame = data.Head
tween(character:WaitForChild("Left Arm"),{CFrame = data.Left})
tween(character:WaitForChild("Right Arm"),{CFrame = data.Right})
remotes.update:FireServer(data)
local offsetCFrame = CFrame.new(character:WaitForChild("Head").CFrame.LookVector)
cam.CFrame = (cam.CFrame:ToWorldSpace(CFrame.new(offsetCFrame.X/3.5*move, 0, offsetCFrame.Z/3.5*move)))
end)
else
print("Uh, sorry, but you can\'t use this script without VR.")
end
function chatted(player, message)
print(player.Name .. " ".. message)
end
uis.InputChanged:Connect(function(input, processed)
if input.UserInputType == Enum.UserInputType.Gamepad1 then
if input.KeyCode == Enum.KeyCode.Thumbstick1 then
move = input.Position.Y
end
end
end)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.ButtonR1 then
holdingRight = true
elseif input.KeyCode == Enum.KeyCode.ButtonL1 then
holdingLeft = true
elseif input.KeyCode == Enum.KeyCode.ButtonB then
remotes.summon:FireServer("M4A1")
elseif input.KeyCode == Enum.KeyCode.ButtonY then
remotes.summon:FireServer("Pistol")
elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
trc = true
elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
tlc = true
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.ButtonR1 then
holdingRight = false
elseif input.KeyCode == Enum.KeyCode.ButtonL1 then
holdingLeft = false
elseif input.KeyCode == Enum.KeyCode.ButtonR2 then
trc = false
elseif input.KeyCode == Enum.KeyCode.ButtonL2 then
tlc = false
end
end)
game:GetService("Players").PlayerAdded:Connect(function(p)
p.Chatted:Connect(function(msg)
chatted(p, msg)
end)
end)
for _, p in pairs(game:GetService("Players"):GetChildren()) do
p.Chatted:Connect(function(msg)
chatted(p, msg)
end)
end
local StarterGui = game:GetService("StarterGui")
--StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)
StarterGui:SetCore("VRLaserPointerMode", 0)
StarterGui:SetCore("VREnableControllerModels", false)
local playerModule = require(game:GetService("Players").LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
local VRFolder = workspace.CurrentCamera:WaitForChild("VRCorePanelParts", math.huge)
while wait(0) do
pcall(function()
VRFolder:WaitForChild("UserGui", math.huge).Parent = nil
playerModule:GetControls():Disable()
end)
end
]], p.Character)