Shop script not working with no error

edit: this might help:

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) 

shop script above

my tools are all as so:
Annotation 2021-11-15 183647

and lastly my button script:
\

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.ToolEvents.SlingShotEvent:FireServer()
end)

and this is the points script

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

One clarification, are you setting the cash leaderstat value to 100 manually when in the game?

yes
it shouldnt affect anything though

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.

image

i tried playing the actual game though and in the actual published game it wouldnt work either.

In the actual published game, did you use the developer console script to set the leaderstat value?

no i clicked the bricks (which increases your points value)

anyways i did what you said and it worked but why wont it work in piblished

I think we are on the right track! Can you show the script for your bricks?

while wait() do 
	wait(1)
	script.Parent.Text = 'loading... 1 '
	wait(1)
	script.Parent.Text = 'loading... 2 '
	wait(1)
	script.Parent.Text = 'loading... 3 '
	script.Parent.Text = 'error loading '
	local Part = Instance.new("Part")
	Part.Parent = game.Workspace
	Part.BrickColor = BrickColor.Random()
	Part.Material = 'Metal'
	local A = math.random(-500,500)
	local B = 0
	local C = math.random(-500,500)
	Part.Position =  Vector3.new(A, B, C)
	local pos = Part.Position
	script.Parent.Text = tostring(Part.Position)

	local clickDetector = Instance.new("ClickDetector")
	clickDetector.Parent = Part

	clickDetector.MouseClick:Connect(function(player)
		script.Parent.Text = (player.Name.. " clicked the brick")
		player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1 
		Part:Remove()
	end)
end

Is this in a LocalScript under a GUI object?

yea

edit i see you typing and then stop and its worrying me lol

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.

what should i change the script to? to be honest i dont really know any other way of making a shop and this method has worked in the past

Give me a moment to type it out. In the meanwhile I suggest you peruse those links I sent.

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

Here’s a step-by-step process of how I would approach this.

  1. In the Explorer window, hit the plus sign and create a “RemoteEvent”. Rename this RemoteEvent to “updateCash”.
    image
  2. In ServerScriptStorage, create a new Script and paste the following code.
game.ReplicatedStorage.updateCash.OnServerEvent:Connect(function(player)
	player.leaderstats.Cash.Value = player.leadrstats.Cash.Value + 1
end)


3. On line 25 of your LocalScript for the brick you sent me, you have written

player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1

CHANGE this to:

game:GetService("ReplicatedStorage"):FindFirstChild("updateCash"):FireServer()
  1. 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.
    image

Finally, hit play! Your code should work. Let me know if you experience any errors.

thanks it worked. you script had a typo but i corrected it and bam worked

1 Like

Awesome! Great to hear. Best of luck on your future coding endeavors.