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

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

Asked by 1 year ago

(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

Log in to vote
1
Answered by 1 year ago

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)
Ad

Answer this question