I am trying to create a script that gives the player a certain amount of WalkSpeed depending on an IntValue when the character respawns. This feature works, but when I try and use a RemoteEvent to send it back to the LocalScript to update the player’s gui it stops working. No errors appear in the output.
Server Script
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(character)
if plr:FindFirstChild("Values") then
local hum = character:FindFirstChild("Humanoid")
hum.WalkSpeed = plr.Values.WalkSpeed.Value
rs:WaitForChild("UpdateGui"):FireClient(plr)
end
end)
end)
Local Script
local rs = game:GetService("ReplicatedStorage")
local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local gui = plr.PlayerGui:WaitForChild("ValueGui")
local textLabel = gui.SpeedBackground.SpeedValue
local function updateGui()
print("test")
if hum.WalkSpeed == 120 then
textLabel.Text = "Speed: "..hum.WalkSpeed.." (MAX)"
else
textLabel.Text = "Speed: "..hum.WalkSpeed
end
end
rs:WaitForChild("UpdateGui").OnClientEvent:Connect(updateGui)
The script never enters the function so I’m assuming that something is wrong within the RemoteEvent.
Im not sure why it is not working. Maybe try changing the Instance “char” to character and remove the :waitforchild() function for the remote event, cuz its already there.
Oh also check for errors in the output
Change the local script to this:
local rs = game:GetService("ReplicatedStorage")
local plr = game.Players.LocalPlayer
local char = plr.Character
local hum = char:WaitForChild("Humanoid")
local gui = plr.PlayerGui:WaitForChild("ValueGui")
local textLabel = gui.SpeedBackground.SpeedValue
local function updateGui()
if hum.WalkSpeed == 120 then
textLabel.Text = "Speed: "..hum.WalkSpeed.." (MAX)"
else
textLabel.Text = "Speed: "..hum.WalkSpeed
end
end
rs.UpdateGui.OnClientEvent:Connect(updateGui)
Server Script:
game.Players.PlayerAdded:Connect(function(plr)
if player then
plr.CharacterAdded:Connect(function(character)
if plr:FindFirstChild("Values") then
local hum = character:FindFirstChild("Humanoid")
hum.WalkSpeed = plr.Values.WalkSpeed.Value
rs:WaitForChild("UpdateGui"):FireClient(plr)
end
end)
end
end)
Your scripts do little to nothing to check if the character (or player) already exists. This may be the root cause of your issue.
You could try this:
ServerScript:
local function added(plr)
local function handle(character)
if plr:FindFirstChild("Values") then
local hum = character:WaitForChild("Humanoid")
hum.WalkSpeed = plr.Values.WalkSpeed.Value
rs:WaitForChild("UpdateGui"):FireClient(plr)
end
end
local character = plr.Character
plr.CharacterAdded:Connect(handle)
if character then
task.spawn(handle, character)
end
end
game.Players.PlayerAdded:Connect(added)
for _, plr in game.Players:GetPlayers() do
task.spawn(added, plr)
end
LocalScript:
local rs = game:GetService("ReplicatedStorage")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local gui = plr.PlayerGui:WaitForChild("ValueGui")
local textLabel = gui.SpeedBackground.SpeedValue
local function updateGui()
print("test")
if hum.WalkSpeed == 120 then
textLabel.Text = "Speed: "..hum.WalkSpeed.." (MAX)"
else
textLabel.Text = "Speed: "..hum.WalkSpeed
end
end
rs:WaitForChild("UpdateGui").OnClientEvent:Connect(updateGui)