So, I am trying to make some basic VR support for myself in roblox studio to mess around with. Which is based from nathandecool1234’s community tutorials. And I have some problems.
-
With it I just want to achieve something I can mess around in for myself with objects and such.
-
The issue is that it is only client-sided. Which is what I need help with.
-
I have tried to simply code a few things but they result in some weird errors. For the server I copied the instancing method from the local-script into the server script, and passing the character as the parent through a remote-event. For all of the fails I also passed the hand CFrame from the local-script to the server-script to try set the CFrame.
a) Has one or both server hands spawning inside the default character. Not very visible but its there.
b) There is some hands still inside of the character, but there are some server hands spawning on the start location from the local ones. But they don’t update.
c) Still hands inside of the character, but there is a bunch of hands being instanced in-general.
the client-script
----------------------------
-- Services and gameobjects.
local VRService = game:GetService("VRService")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local StarterGui = game:GetService("StarterGui")
local Players = game:GetService("Players")
local Camera = game.Workspace.CurrentCamera
local Player = Players.LocalPlayer
local Character = Player.Character
local ReplicateHands = game.ReplicatedStorage.ReplicateHands
local TeleportingDistance = 100
local RightSendingCFrame
local LeftSendingCFrame
---------
-- Bools.
local teleportPressed = false
---------------------------------------------------------------------------
-- Camera properties, disable default movement and freeze non-VR character.
Camera.CameraType = Enum.CameraType.Scriptable
Camera.HeadScale = 1
Camera.CFrame = CFrame.new(Camera.CFrame.Position)
StarterGui:SetCore("VRLaserPointerMode", 0)
StarterGui:SetCore("VREnableControllerModels", 0)
Character.HumanoidRootPart.Anchored = true
-----------------------------
-- Creating the player hands.
local function createHand(handType)
local hand = Instance.new("Part")
hand.CFrame = Character.HumanoidRootPart.CFrame
hand.Size = Vector3.new(.4, .4, 1)
hand.Transparency = 0
hand.CanCollide = false
hand.Anchored = true
hand.Name = handType
hand.Color = Color3.new(1, .72, .6)
hand.Material = Enum.Material.Plastic
hand.TopSurface = Enum.SurfaceType.Smooth
hand.BottomSurface = Enum.SurfaceType.Smooth
hand.Parent = Character
return hand
end
local RightHand = createHand("RightVRHand")
local LeftHand = createHand("LeftVRHand")
--------------------------
-- Functions to be called.
local function TeleportToDirectedPoint()
local origin = RightHand.Position
local direction = RightHand.CFrame.LookVector * TeleportingDistance
local raycastResult = game.Workspace:Raycast(origin, direction, RaycastParams.new())
if raycastResult then
if raycastResult.Instance.Parent ~= Character and raycastResult.Normal.Y > .4 then
local cameraAngles = Camera.CFrame - Camera.CFrame.Position
local headCFrame = VRService:GetUserCFrame(Enum.UserCFrame.Head)
Camera.CFrame = CFrame.new(raycastResult.Position + Vector3.new(0, 5 + headCFrame.Position.Y, 0) - headCFrame.Position) * cameraAngles
end
end
end
----------------------------
-- Up and running functions.
function handCorrection(part, move)
local newCFrame = Camera.CFrame * move
if part == Enum.UserCFrame.RightHand then
RightHand.CFrame = newCFrame
RightSendingCFrame = RightHand.CFrame
elseif part == Enum.UserCFrame.LeftHand then
LeftHand.CFrame = newCFrame
LeftSendingCFrame = LeftHand.CFrame
end
ReplicateHands:FireServer(Character, RightSendingCFrame, LeftSendingCFrame)
end
function inputHandling(key, processed)
if key.UserInputState == Enum.UserInputState.Begin then
if key.KeyCode == Enum.KeyCode.ButtonB then
teleportPressed = true
end
end
if key.UserInputState == Enum.UserInputState.End then
if key.KeyCode == Enum.KeyCode.ButtonB then
teleportPressed = false
TeleportToDirectedPoint()
end
end
end
function rightHandIndicator()
if teleportPressed then
local origin = RightHand.Position
local direction = RightHand.CFrame.LookVector * TeleportingDistance
local raycastResult = game.Workspace:Raycast(origin, direction, RaycastParams.new())
if raycastResult then
if raycastResult.Instance.Parent ~= Character then
local hit = Instance.new("Part")
hit.Material = Enum.Material.Neon
hit.Anchored = true
hit.Position = raycastResult.Position
hit.Size = Vector3.new(.4, .4, .4)
hit.Parent = Character
RunService.Heartbeat:Wait()
hit:Destroy()
end
end
end
end
function leftHandIndicator()
if not nil then
local origin = LeftHand.Position
local direction = LeftHand.CFrame.LookVector * TeleportingDistance
local raycastResult = game.Workspace:Raycast(origin, direction, RaycastParams.new())
if raycastResult then
if raycastResult.Instance.Parent ~= Character then
local hit = Instance.new("Part")
hit.Material = Enum.Material.Neon
hit.Anchored = true
hit.Position = raycastResult.Position
hit.Size = Vector3.new(.4, .4, .4)
hit.Parent = Character
RunService.Heartbeat:Wait()
hit:Destroy()
end
end
end
end
------------------------
-- Function connections.
UserInputService.InputBegan:Connect(inputHandling)
UserInputService.InputEnded:Connect(inputHandling)
UserInputService.UserCFrameChanged:Connect(handCorrection)
RunService.RenderStepped:Connect(rightHandIndicator, leftHandIndicator)
VRService:RecenterUserHeadCFrame()
all the server-sided code. Most of all are the same with minor differences.
a)
local event = game.ReplicatedStorage.ReplicateHands
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local RightHand
local LeftHand
local RightHandCFrame
local LeftHandCFrame
event.OnServerEvent:Connect(function(plr, character, RightCFrame, LeftCFrame)
local function createHand(handType)
local hand = Instance.new("Part")
hand.CFrame = character.HumanoidRootPart.CFrame
hand.Size = Vector3.new(.4, .4, 1)
hand.Transparency = 0
hand.CanCollide = false
hand.Anchored = true
hand.Name = handType
hand.Color = Color3.new(1, .72, .6)
hand.Material = Enum.Material.Plastic
hand.TopSurface = Enum.SurfaceType.Smooth
hand.BottomSurface = Enum.SurfaceType.Smooth
hand.Parent = character
return hand
end
RightHand = createHand("RightVRHand")
LeftHand = createHand("LeftVRHand")
RightHandCFrame = RightCFrame
LeftHandCFrame = LeftCFrame
end)
RunService.Heartbeat:Connect(function()
RightHand.CFrame = RightHandCFrame
LeftHand.CFrame = LeftHandCFrame
RunService.Heartbeat:Wait()
RightHand:Destroy()
LeftHand:Destroy()
end)
b)
local event = game.ReplicatedStorage.ReplicateHands
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local RightHand
local LeftHand
event.OnServerEvent:Connect(function(plr, character, RightCFrame, LeftCFrame)
local function createHand(handType)
local hand = Instance.new("Part")
hand.CFrame = character.HumanoidRootPart.CFrame
hand.Size = Vector3.new(.4, .4, 1)
hand.Transparency = 0
hand.CanCollide = false
hand.Anchored = true
hand.Name = handType
hand.Color = Color3.new(1, .72, .6)
hand.Material = Enum.Material.Plastic
hand.TopSurface = Enum.SurfaceType.Smooth
hand.BottomSurface = Enum.SurfaceType.Smooth
hand.Parent = character
return hand
end
RightHand = createHand("RightVRHand")
LeftHand = createHand("LeftVRHand")
RunService.Heartbeat:Connect(function()
RightHand.CFrame = RightCFrame
LeftHand.CFrame = LeftCFrame
RunService.Heartbeat:Wait()
RightHand:Destroy()
LeftHand:Destroy()
end)
end)
c)
local event = game.ReplicatedStorage.ReplicateHands
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local RightHand
local LeftHand
event.OnServerEvent:Connect(function(plr, character, RightCFrame, LeftCFrame)
local function createHand(handType)
local hand = Instance.new("Part")
hand.CFrame = character.HumanoidRootPart.CFrame
hand.Size = Vector3.new(.4, .4, 1)
hand.Transparency = 0
hand.CanCollide = false
hand.Anchored = true
hand.Name = handType
hand.Color = Color3.new(1, .72, .6)
hand.Material = Enum.Material.Plastic
hand.TopSurface = Enum.SurfaceType.Smooth
hand.BottomSurface = Enum.SurfaceType.Smooth
hand.Parent = character
return hand
end
RightHand = createHand("RightVRHand")
LeftHand = createHand("LeftVRHand")
RightHand.CFrame = RightCFrame
LeftHand.CFrame = LeftCFrame
end)
Sorry if I missed some important info or specifics, this is my first time here.