How do I fix this Server Script Player problem?

Hi I’m currently working on a Shop Gui but somehow when there is more than 1 player it will automatically pay double like this:

It is launched from a Local Script and activated in the Server Script.

Local Script:

local Player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	if Player.leaderstats.Rubles.Value >= 10 then
		game.ReplicatedStorage.BusinessEvent.BobGeneralStoreEvent:FireServer()
		script.Parent.Parent.Parent.Parent.Parent.Parent.BobGeneralStore.Enabled = false
	end
end)

Server Script:

local Tool = game.ReplicatedStorage.BusinessItems.JokeGun

game.ReplicatedStorage.BusinessEvent.BobGeneralStoreEvent.OnServerEvent:Connect(function(Player)
	Player.leaderstats.Rubles.Value -= 10
	Tool:Clone().Parent = Player.Backpack
end)

Please add debugging prints to pinpoint where and which code executes twice.

Example:

Local Script

local Player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	print("Clicked.")
	if Player.leaderstats.Rubles.Value >= 10 then
		game.ReplicatedStorage.BusinessEvent.BobGeneralStoreEvent:FireServer()
		print("Fired event.")
		script.Parent.Parent.Parent.Parent.Parent.Parent.BobGeneralStore.Enabled = false
	end
end)

Server Script

local Tool = game.ReplicatedStorage.BusinessItems.JokeGun

game.ReplicatedStorage.BusinessEvent.BobGeneralStoreEvent.OnServerEvent:Connect(function(Player)
	print("Executing event to: ", Player)
	Player.leaderstats.Rubles.Value -= 10
	Tool:Clone().Parent = Player.Backpack
end)

Then follow up with a result of the output window so that we can hopefully find a solution. If this does not clarify anything, at least it’s a clue richer to finding the issue.

1 Like

i just want to say, please add checks for how much money they have on the server otherwise exploiters or unsynced clients can get infinite

other than that do what lemny said

1 Like

It’s probably because Player in the server script is not taking local Player,

You could try this (even if that’s not compact it worked for me)

local Tool = game.ReplicatedStorage.BusinessItems.JokeGun
local Players = game:GetService("Players")
local LocalPlayer 

game.Players.PlayerAdded:Connect(function(plr)
      LocalPlayer = plr
end)

game.ReplicatedStorage.BusinessEvent.BobGeneralStoreEvent.OnServerEvent:Connect(function(Player)
	print("Executing event to: ",)
	LocalPlayer.leaderstats.Rubles.Value -= 10
	Tool:Clone().Parent = LocalPlayer.Backpack
end)

Doesn’t print twice…

It gives to the wrong player.

Using the player-added function is a bad idea; every player that will now join is now considered the local player, which is why it is going to the wrong person. The remote event itself will pass to the player who pressed the button. Player 2 was the last to join, which is why Player 2 got the gun and not Player 1.

Exactly (I hate 30 words limit)

I do not see any debugging prints coming from the ServerScript.
Please add the one I suggested and any other possible debugging.

So far I can see, the LocalScript works as it’s intended to.

I already copied the script you gave. There must’ve been a problem

Then the issue is more than just a coding error. There is a miscommunication in the RemoteEvent being fired. Perhaps a different script is picking up signals being sent to the server. More than this I can not help with the limited access to find out such issues.

Did you already tested the scripts?

Alright, basically right now the problem is the Local Player.

Else you could use a DataStore that work perfectly in local ?

You’re receiving a Player variable from your ServerScript’s connection.

Make a new Remote, ServerScript, and ClientScript for each new PlayerAdded (Important to prevent exploiters from making other players buy things) and then simply take the new player’s player and pass it to the ServerScript.

Upon the event being fired, you can check if the Player variable from the event is the same as the Player variable passed from when the new scripts and events are made. Then, do your leaderboard stuff with either variable.

Wait I still don’t get it a bit…

Anyways you guys can just swap the Server with Local.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.