hello, i wrote a basic shop script but its not working. i programmed a built in error message in case for some reason a normal error message wasnt generated.
currently on my game, my shop script triggered the programmed error message however i dont know what the problem is.
Shop script:
game.ReplicatedStorage.ToolEvents.SwordEvent.OnServerEvent:Connect(function(player)
if player.leaderstats.Cash.Value >= 1 then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 1
game.ServerStorage.Tools.Sword:Clone().Parent = player.Backpack
else
print("not working")
end
end)
local function onPlayerJoin(player)
local leaderstats = Instance.new('Folder')
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("NumberValue")
cash.Name = "Cash"
cash.Value = 0
cash.Parent = leaderstats
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
heres my cash display script
while wait() do
player = game.Players.LocalPlayer
script.Parent.Text = ("$" ..player.leaderstats.Cash.Value)
end
Can somone please help me? this is confusing me a lot becuase it should work
I think I know your error. You are setting the cash value locally instead of globally. To set the value from the server, hit the “Current: Client” at the top, and you will be toggled to the server. From there you can manually change your player’s leaderstats value to 100. Then, change back to the client and try your button out.
There’s your issue.
This all boils down to a matter of discerning between the client and the server. Your shop script (shown below) checks to see if the leaderstat cash value on the SERVER is greater than or equal to 1. However, since you are using a LocalScript, you are incrementing cash on the CLIENT.
game.ReplicatedStorage.ToolEvents.SwordEvent.OnServerEvent:Connect(function(player)
if player.leaderstats.Cash.Value >= 1 then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 1
game.ServerStorage.Tools.Sword:Clone().Parent = player.Backpack
else
print("not working")
end
end)
The whole topic of Server vs. Client is an extremely complex issue in itself, so I suggest taking a look at the following articles for some pointers.
A few guidelines to keep in mind:
Client-side (local) events are handled by a PLAYER’s device.
Server-side (global) events are handled by Roblox servers.
Client actions take place in LocalScripts.
Server actions generally take place in Scripts.
Changing local data will not affect the server (generally speaking). This is what is happening in your case. You are changing the leaderstat cash value on the LOCAL side of things, which has no impact on the server when it looks at the leaderstat value.
Your solution? I would use a RemoteEvent from a LocalScript to fire an event on the server that increments the cash there, since the server needs to change this value. If you do not know what this means, again, I recommend you take a look at the two articles I have pasted above.
i have to take a shower so i will look at your script and the articles later. If the script works i think you will deserve the solution. this helps a lot thanks
Finally, I see on line 30 in your screenshot of the brick LocalScript, you accidentally added a breakpoint. Click on it to get rid of it if you haven’t already done so.
Finally, hit play! Your code should work. Let me know if you experience any errors.