Shop doesn't take 10 coins from my currency

(SLEEPING SO I CAN’T ANSWER)

Hello, so i wanna try to make a shop.
For every second you’re in the game you get 1 coin. here is the code for that:

game.Players.PlayerAdded:Connect(function(player)
	local value = Instance.new("NumberValue", player)
	value.Name = "Currency"
	
	while wait(1) do
		value.Value = value.Value + 1
	end
end)

For the next thing i ‘tried’ to make a button where there will go coins of you’re total.
Here is the script for that:

local player = game.Players.LocalPlayer

script.parent.MouseButton1Click:Connect(function()
	player.Currency.Value = player.Currency.Value - 10
end)

So the problem that I have is if u press the button, there will go 10 coins of you’re total but the second after the 10 coins went gone, they went back.
Is this a problem in my script or do i just need to script more?

If u don’t understand this here is a example:

  • you get every second 1 coin and the sword in the shop cost 10 coins
  • you buy the sword en 10 coins will diseppear
  • the script for the coins that will add every second will just go and if that script adds a coin back, the 10
    coins go back to your currency

hopefully you will understand it now :sweat_smile:

Thanks for reading this and hopefully helping me!

3 Likes

You will need to send a remote event to the server that can administer this transaction.

Check out these articles: Bindable Events and Functions | Roblox Creator Documentation, RemoteEvent | Roblox Creator Documentation

oh, oki!
thank you :grinning_face_with_smiling_eyes:

sorry if this was a dumb question but i’m just a beginner in scripting :sweat_smile:

2 Likes

don’t worry it wasn’t. even if it was who are we to judge you for it

4 Likes

You don’t need to use remote events here, seems like a misunderstanding.

To start off, what you’re trying to do in your second script is remove 10 from the value of the player’s money however as you’re able access Players.LocalPlayer it becomes evident that you’re running this on a local script, therefore any changes that you make within that script (for example, manipulating the money’s value) would only affect the client and would be reverted every time the money value is updated by the server (which happens every second in your first script).

In order to fix this you’d have to transfer your second script to a server script and refactor it a bit like so:

script.parent.MouseButton1Click:Connect(function(player)
	player.Currency.Value = player.Currency.Value - 10
end)

Hope I helped!

2 Likes

So you mean like this?

local player = game.Players.LocalPlayer

script.parent.MouseButton1Click:Connect(function(player)
	player.Currency.Value = player.Currency.Value - 10
end)

Because I have turned it in a server script but nothing happens now.

I even get this error message:
Players.debestevanal.PlayerGui.Shop.Frame.Frame.TextButton.Script:4: attempt to index nil with ‘Currency’

(And sorry for the late response)

1 Like

It’s because the scripts are fighting against each other. you set the value to value -10 and then the script sets it to the former value +1. you can pause the given script for 2 seconds and that should fix it.

1 Like

not a HUGE deal but just a heads up, you can also save some time bye doing
player.Currency.Value -= 10
or even
player.Currency.Value += 1

saves some time but it does the same stuff :stuck_out_tongue:

But that doesn’t solve my problem? :face_with_raised_eyebrow:

Okay, do this.

Put a remote function in game.ReplicatedStorage and name it “Transaction”.
Here are the scripts:

Server Script:

local remoteFunction = game.ReplicatedStorage.Transaction

remoteFunction.OnServerInvoke = function(player, amt)
	
	if player.Currency.Value >= math.abs(amt) then
		player.Currency.Value -= math.abs(amt)
		return "Successful"
	else
		return "Unsuccessful"
	end
end

Local Script:

local remoteFunction = game.ReplicatedStorage.Transaction
local player = game.Players.LocalPlayer

script.parent.MouseButton1Click:Connect(function(player)
	print(remoteFunction:InvokeServer(10))
end)

Didn’t test this on studio so lmk if it works, it should though.

4 Likes

Hey there,
Sorry for the late response but it worked :smile:

Thank you for helping me and i hope you have a good day

2 Likes