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

Cash only being removed locally?

I have a shop script, but when an item is purchased it only removes the players cash locally and not server sided, may i have some help with this issue? Thanks.

Remote Function

local gungiveEvent = game.ReplicatedStorage.GunGiveEvent

gungiveEvent.OnServerEvent:connect(function(player, gun)

	
	if game.ServerStorage:FindFirstChild(gun) then
		local gun = game.ServerStorage:FindFirstChild(gun):Clone()
		wait(1)
		gun.Parent = player.Backpack
		
		wait(999)
		end
	end)

Script in the text button



local event = game.ReplicatedStorage.GunGiveEvent
script.Parent.MouseButton1Click:connect(function()
	if game.Players.LocalPlayer.leaderstats.Cash.Value >= 100 then
for i,v in pairs(game.Players:GetPlayers()) do
if v:FindFirstChild("leaderstats") then
v.leaderstats.Cash.Value = v.leaderstats.Cash.value - 100

	event:FireServer("Kar98k")
end
end
end
end)

8 comments

0

I'm assuming the Script in the Text Button is Local yes?

0

If not, that's an issue, also, it would be adding directly to the Client anyway as it was told to, you have to use the ServerScript (ServerScriptService) to add the cash, from the Server.

1

ServerScripts and LocalScripts run in different environments; LocalScripts will only run directly for the Client, ServerScripts will only run directly for the Server. You need to use a ServerScript to have the desired work completed on the Server

0

so how would that be accomplished, because to my knowledge the RemoteEvent i have can only be triggered from a local script?

0

The code I have in my answer shows how to accomplish this. To make it easier to read you may have to hover over it and click "view source."

1

Since these Scripts' vulnerable, automatic communication, was disrupted, you have to manually communicate further on. RemoteEvents are used to Fire to the counterpart of the Script it's being used in, so you'd generically have to Fire from the LocalScript, yes.

0

Thanks to both of you guys, i appreciate your help! :)

0

No problem, and welcome to Scripting Helpers! If my answer helped please accept it so both of us can gain reputation :)

1 answer

✓ Accepted answer

The cash is only being removed locally because you placed the lines of code that removes the player's cash in the local script instead of the server script.

Moving the lines of code that removes the player's cash into the server script will resolve this.

SERVER:

local gungiveEvent = game.ReplicatedStorage.GunGiveEvent

gungiveEvent.OnServerEvent:connect(function(player, gun)
	if game.ServerStorage:FindFirstChild(gun) then --check if we have the gun
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 100
        	local gun = game.ServerStorage:FindFirstChild(gun):Clone()
        	wait(1)
        	gun.Parent = player.Backpack

        	wait(999)
		end
        end
end)

LOCAL:

local event = game.ReplicatedStorage.GunGiveEvent

script.Parent.MouseButton1Click:connect(function()
	if game.Players.LocalPlayer.leaderstats.Cash.Value >= 100 then
    		event:FireServer("Kar98k")
	end
end)

3 comments

0

Yes it is a local script, and i dont quite understand what you mean buy "you have to use the serverscript to the cash, from the server". could you maybe go slightly more in depth?

0

If you use a local script, anything ran in that local script will only affect the player that's running it. For example, if you made the player running the script invisible, only that player would see themselves as invisible. If you used a server script to make that player invisible, everyone would see that player as invisible. Feahren's comments above explain this more technically.

1

:) This is caused by the enforcement of FilteringEnabled, this tells the Server to reject replication to all other connected Clients, of the Local Client's actions. This causes the effect of being unable to see changes brought to come by other Clients vice-versa.

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