Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why doesn't the server script always give the value "nil" to the client?

Asked by 5 years ago

This is the first client script

TransformEv = game.ReplicatedStorage.Events:FindFirstChild("TransformEv")
local pobo = 2
UserInputService.InputBegan:connect(function(input,gameProcessed)
if input.UserInputType == Enum.UserInputType.Keyboard and not gameProcessed then
if input.KeyCode == Enum.KeyCode.F then
     TransformEv:FireServer({Type = "Transform",Choice = pobo}) 
end
end
end)

It sends the value to the server script

local CameraChange = game.ReplicatedStorage.Events.CameraChange
game.ReplicatedStorage.Events.TransformEv.OnServerEvent:connect(function(player,args)
local char = player.Character
if args.Type == "Transform" then
    local Stats = player:WaitForChild("Stats")
    local OtherPlayer = args.Choice
    Stats.Transformed.Value = true
    CameraChange:FireClient(player,OtherPlayer)
end
end)

Then it sends it back to the other client script

local CameraChange = rs.Events:WaitForChild("CameraChange")
function CameraOnPlayer(player,OtherChar)
    cam.CameraSubject = workspace:FindFirstChild(OtherChar):FindFirstChild("Torso")
end
CameraChange.OnClientEvent:Connect(CameraOnPlayer)

The problem is the server script won't give the value to the client. I printed it and the server does have the value but every time I try and send the value to the client it is nil.

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

The issue is actually pretty simple. FireClient does not pass the player that the RemoteEvent/RemoteFunction is called for. This is why LocalPlayer exists, to tell you who is the local client. Removing the player argument should fix the issue.

local CameraChange = rs.Events:WaitForChild("CameraChange")
function CameraOnPlayer(OtherChar)
    cam.CameraSubject = workspace:FindFirstChild(OtherChar):FindFirstChild("Torso")
end CameraChange.OnClientEvent:Connect(CameraOnPlayer)
0
I love you, Thank you. User#12423 0 — 5y
Ad

Answer this question