My code executes for every player in the sever when I only want it to execute for the player that fired the event. When one person touches a checkpoint that point not only goes to the player themselves but to everyone on the server.
Local player script:
local rep = game:GetService("ReplicatedStorage")
local game_workspace = game:GetService("Workspace")
local remoteevent = rep:WaitForChild("remoteevent")
local folder = game_workspace:WaitForChild("Spawners")
local player = game.Players.LocalPlayer
local players_data = {}
players_data[player.Name] = {} -- Stores checkpoints
print(players_data)
---- Function Declarations ---
local function get_player_stats(character)
--Gets access to players Obby level value--
local leaderstats = player:FindFirstChild("leaderstats")
return leaderstats:WaitForChild("Obbylvl")
end
local function peruse(spawner)
---Verify whether spawner can be touched or not
-- True = found in the list
--False = not found in the list
local checker = true
for key, players_checkpoint in pairs(players_data) do
for _, element in ipairs(players_checkpoint) do
if element == spawner then
return checker
end
end
end
checker = false
return checker
end
local function append(spawner)
--Add the new spawner to the player's checkpoint list
for _, players_checkpoint in pairs(players_data) do
table.insert(players_checkpoint, spawner)
end
print("Added to your list")
end
---End of Function Declaration -----
for i, spawner_object in ipairs(folder:GetChildren()) do
spawner_object.Touched:Connect(function(hit)
local player_value = get_player_stats(hit.Parent)
if peruse(spawner_object) == false then
append(spawner_object)
remoteevent:FireServer(players_data)
end
end)
end
Server Script
-- Declaration of variables
local rep = game:GetService("ReplicatedStorage")
local rem = rep:WaitForChild("remoteevent")
local function tally(player,player_data)
---Updating player's obby level value---
local leaderstats = player:FindFirstChild("leaderstats")
local player_value = leaderstats:FindFirstChild("Obbylvl")
for _, v in pairs(player_data) do
player_value.Value = #v
end
end
rem.OnServerEvent:Connect(function(player, player_data)
tally(player, player_data)
end)