basically i have a datastore script, and a localscript that sends a remoteevent what i want is for the value to only be given to the player who activated the RemoteEvent
If the the remote if being fired from the client, then the server knows what client is firing the event. It will be that player you are handling on the server. You can even filter the player that fires the event.
CountTime.OnServerEvent:Connect(function(plr)
local SaveFile = plr.SaveFile
local Time0 = plr.Time
local Time1 = plr.SaveDataFile1.Time1
local Time2 = plr.SaveDataFile2.Time2
local Time3 = plr.SaveDataFile3.Time3
while task.wait(1) do
if SaveFile.Value == 1 then
Time1.Value += 1
Time0.Value += 1
elseif SaveFile.Value == 2 then
Time2.Value += 1
Time0.Value += 1
elseif SaveFile.Value == 3 then
Time3.Value += 1
Time0.Value += 1
elseif SaveFile.Value == 0 then
print("Pizza Time")
end
end
end)
when i test this script with more then 1 players it gives a 1 for each player in the game
That doesn’t seem like it should be the case. Is it possible the remote event is firing for every player unintentionally?
could be here’s the script everyone has the button at the start of the game
local player = game.Players.LocalPlayer
local Saves = player:WaitForChild("SaveFile")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CountTime = ReplicatedStorage:WaitForChild("CountTime")
script.Parent.MouseButton1Click:Connect(function()
CountTime:FireServer()
end)
this is the base script I just removed everything else not connect to the thing were talking about
Yeah that looks correct too–that shouldn’t fire for every player.
Maybe try running it with this code to debug:
CountTime.OnServerEvent:Connect(function(plr)
-- You probably want to check if the player has already started a loop here, otherwise they can start multiple (e.g. 2,3, 100 updates per second)
-- Should only run for the player who pressed
print("Remote event received for "..plr.Name.."!")
local SaveFile = plr.SaveFile
local Time0 = plr.Time
local Time1 = plr.SaveDataFile1.Time1
local Time2 = plr.SaveDataFile2.Time2
local Time3 = plr.SaveDataFile3.Time3
while task.wait(1) do
if SaveFile.Value == 1 then
Time1.Value += 1
Time0.Value += 1
elseif SaveFile.Value == 2 then
Time2.Value += 1
Time0.Value += 1
elseif SaveFile.Value == 3 then
Time3.Value += 1
Time0.Value += 1
elseif SaveFile.Value == 0 then
print("Pizza Time")
end
end
end)
Ok it prints ““Received For Player 2 (x5)” which means it activated 5 times (had 5 players in-game”
I think what happend is that it FireServer() it fires for every player in the game, yes I know shouldn’t but that seems to be what happens
btw I am using Roblox’s Test Players don’t think that’s anything to do with it, but could be a possibility
That’s super confusing. I’m not sure what to tell you. It’s weird that it fires for each of the players in the server for just the player who clicked (eg player 2).
The plr arg passed is an Instance, so you’re right it shouldn’t matter if it’s test characters.
Let me try this in studio really fast and see if I get the same thing.
Ah I know what the problem is. Where is the server script located? It’s probably in StarterPlayerScripts or StarterCharacterScripts, so the game makes duplicates of the code for every player in the game.
That means when the event fires, it gets received by each player’s copy of the script, resulting in duplicate responses.
The server script should be in ServerScriptService or Workspace, somewhere it won’t get duplicated. Sometimes you want duplicates, like when the script only works with a single player (eg uses script.Parent.Parent to get a player), but for stuff like this you want a single copy.
I have put the Server script in ServerScriptService, that’s what it original was
I tested the code and it worked as expected for me:
13:12:52.139 ▶ Remote event received for Player1! (x2) - Server - Script:4
13:13:04.637 Remote event received for Player2! - Server - Script:4
(I pressed it twice for the first player and then once for the second.)
Maybe check out this place file and see if there are any differences:
Baseplate DevForum 2850130.rbxl (58.2 KB)
Maybe some of the code that was omitted causes a problem. I’d double check that the script isn’t being duplicated for each player and that the connection isn’t created inside something like OnPlayerAdded etc.
I figured it out basically when you said OnPlayerAdded i spilt the script in two, one for creating the values with OnPlayerAdded, and then one for giving values, thanks for the help. only thing is my save script is in the value creating script but i’ll figur something out, thanks for the help
(Adding this for the solved thing)
Spilt the script into Value Maker and Value Giver ok
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.