Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[SOLVED] My code to purchase items is giving me extra money. Why?

Asked by 5 years ago
Edited 5 years ago

So I created 2 scripts, one where it gives you 10 cash every 10 seconds and one script where when you click a button it gives you a snowball and it subtracts 10 cash from you.

Whenever I purchase the snowball, when the cash increase script loops back, it gives me 20 cash. If I purchase 10 snowballs it gives me 110 cash. Why is this happening??

Here is the script for my cash increase:

-- Variables --
mintime = 10

-- Coding --

local function onPlayerJoin(player)
    while true do
        wait(mintime)
        local cash = player.leaderstats.Cash
        cash.Value = cash.Value + 10
    end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

Here is the script for my purchasing: The value for the price is 10 and the value for the item is Snow ball.

button = script.Parent
item = script.Parent.ItemInGame.Value
price = script.Parent.Price.Value

script.Parent.ItemInGame.Changed:Connect(function()
    item = script.Parent.ItemInGame.Value
end)

script.Parent.Price.Changed:Connect(function()
    price = script.Parent.Price.Value
end)

button.MouseButton1Click:connect(function()
    local player = game.Players.LocalPlayer
    if player.leaderstats.Cash.Value >= price then
        player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - 10
        game.Lighting[item]:Clone().Parent = player.Backpack
    elseif player.leaderstats.Cash.Value < price then
        button.Text = "Not Enough Money!"
        wait(1.5)
        button.Text = "Purchase"
    end
end)
0
You told it to add not subtract the price iiDev_Hunt3r 64 — 5y
0
Where? TypicallyPacific 61 — 5y
0
If you're talking about the first script, that script is supposed to give the player 10 cash every 10 seconds automatically. TypicallyPacific 61 — 5y
0
You should not be storing tools in lighting or any place the client has access. Also you should not me changing the values in a local script User#5423 17 — 5y
View all comments (7 more)
0
as kingdom said you cant change cash in a local script, you'll need to use a server script or a remote event DinozCreates 1070 — 5y
0
If I do it in a normal script it doesn't work. The first one was already a normal script but the second one was a local script. I made it a normal script and it doesn't work. TypicallyPacific 61 — 5y
0
of course it didnt. the easiest way will be to use a remote event on line 16 where you're removing the cash. DinozCreates 1070 — 5y
0
I am using values so it knows what item the player is purchasing, how would the remote event know what item is being purchased? TypicallyPacific 61 — 5y
0
you pass the information DinozCreates 1070 — 5y
0
Thanks! It worked. TypicallyPacific 61 — 5y
0
please mark as [Solved] DinozCreates 1070 — 5y

Answer this question