Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't the code in my remote event run?

(I made a custom leaderboard)

I have created a remote event that runs everytime a player leaves or joins the game. I added a script which adds the player to the leaderboard, but the code won't run. No errors come into the console either.

Local Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoveRemoteEvent = ReplicatedStorage:WaitForChild("LeaderboardRemovePlayer")
local AddRemoteEvent = ReplicatedStorage:WaitForChild("LeaderboardAddPlayer")

game.Players.PlayerAdded:Connect(function()
	AddRemoteEvent:FireServer()
end)

game.Players.PlayerRemoving:Connect(function()
	RemoveRemoteEvent:FireServer()
end)

Server Script

local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local RE = game:GetService("ReplicatedStorage"):WaitForChild('LeaderboardAddPlayer')


RE.OnServerEvent:Connect(function()
	for i,v in pairs(Players:GetPlayers()) do
		ServerStorage:WaitForChild("PlayerThing").Parent = v.PlayerGui:WaitForChild("Leaderboard"):WaitForChild("PlayerList")
	end
	RE:FireAllClients()
end)

1 answer

✓ Accepted answer

Instead of running the PlayerAdded code on your client (which would call the server several times if you have more than one player), why not do it in the server?

local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local RE = game:GetService("ReplicatedStorage"):WaitForChild('LeaderboardAddPlayer')

local function onPlayerAdded()
    for i,v in pairs(Players:GetPlayers()) do
        ServerStorage:WaitForChild("PlayerThing").Parent = v.PlayerGui:WaitForChild("Leaderboard"):WaitForChild("PlayerList")
    end
    RE:FireAllClients()
end

local function onPlayerDisconnect()
    -- do something when the player disconnects
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerDisconnect)

This discussion is preserved as it was. New answers, comments, and votes are closed.