I’m trying to reload my player, I can get it to work in Studio, but not in game.
I checked output and everything seems to be working how it should in game, other than it won’t reload the player.
Here is my code: (this is a Script in ServerScriptStorage)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local loadPlayer_RE = ReplicatedStorage:WaitForChild("LoadPlayer")
function loadPlayer(player)
print(player.Name, "got respawned")
player:LoadCharacter()
end
loadPlayer_RE.OnServerEvent:Connect(loadPlayer)
Besides the fact that you shouldn’t let clients request respawns whenever without rate limiting them, and the oddly inconsistent variable naming, can we see the client code?
local plr = game:GetService("Players").LocalPlayer
local buttons = plr:WaitForChild("PlayerGui"):WaitForChild("MainUI"):WaitForChild("Img"):WaitForChild("Levels"):WaitForChild("Holder"):GetChildren()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LoadPlayer = ReplicatedStorage:WaitForChild("LoadPlayer")
for _, button in next, buttons do
if button.Name ~= "UIGridLayout" then
button.TextButton.MouseButton1Click:Connect(function()
LoadPlayer:FireServer()
end)
end
end
Also, I’m not sure how to limit the requests.
And what would be better names?
My personal preference is having one remote event and firing it with the first argument being a “mode” string to distinguish the intent. From there, you can make a server side table of players and the tick() their last remote firing was. Each time a player fires the sever, check that player’s last firing time against the current tick() to see if they’re firing with too small of a frequency.
I mentioned the variable names because some are written in camelCase (first letter small) and some are written with capital first letters. It’s your choice which to use but it looks cleaner if you pick one.
EmeraldSlash already got to this, but to expand, your error is probably in that for statement. I’m not familiar with the next keyword, but try replacing the first line of that for statement with this:
My LocalScript (One that fires it) is in a UI in StarterGui,
The Server-Side Script is in ServerScriptService,
The RemoteEvent is in ReplicatedStorage.
Your server Sided Code and Client Sided work completely fine for me in studio with the only change being this
local plr = game:GetService("Players").LocalPlayer
local buttons = plr:WaitForChild("PlayerGui"):WaitForChild("MainUI"):WaitForChild("Img"):WaitForChild("Levels"):WaitForChild("Holder"):GetChildren()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LoadPlayer = ReplicatedStorage:WaitForChild("LoadPlayer")
for _, button in pairs (buttons) do
if button.Name ~= "UIGridLayout" then
button.TextButton.MouseButton1Click:Connect(function()
LoadPlayer:FireServer()
end)
end
end
Using in pairs instead of in next and having () surrounding “buttons” very minor changes, doesn’t really explain why its not working for you.
Edit: Also works when publishing to Roblox and playing.