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
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:
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.
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.