Can't get LoadCharacter() to work

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)
3 Likes

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?

2 Likes
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?

1 Like

Seems to me like the issue is probably with your local code. Have you tried printing when it is clicked and when it is received on the server?

1 Like

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:

for _,button in pairs(buttons) do

2 Likes

It’s clicked at 14:55:12 and received at 14:55:11.

1 Like

Is your server side print after the LoadCharacter call?

1 Like

and I replaced the first line with what you suggested, still same error.

1 Like

No.

1 Like

Will it still print if you put it after?

1 Like

Tried it, and it still did.

1 Like

Where is your remote and server-side script located?

1 Like

My LocalScript (One that fires it) is in a UI in StarterGui,
The Server-Side Script is in ServerScriptService,
The RemoteEvent is in ReplicatedStorage.

1 Like

Any reason for using next? rather then just using pairs.

1 Like

I tried using pairs and I had the same issue.

I got a workaround by using “MoveTo” x location instead of completely respawning (although respawning would work better, but it’s a temporary fix)

1 Like

Pairs returns the next iterator, so there really isn’t a difference.

2 Likes

Does the print appear in the Developer Console In-game?

1 Like

Yes, all prints appear in game.

1 Like

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.

1 Like