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

Why does House Purchase script keeps setting Cash Value to 500?

Asked by 4 years ago
Edited 4 years ago

I'm making a game and one of the aspects in it is you can buy a house for 1000 "Cash" and I have a cash debug in the game to give me cash. If i set my Cash to above 1500 (starting cash) it always sets it to 500 instead of (for example) 501

EDIT I think the cash is not server side, I'll test this theory

My code:

local houseprice = 1000 -- The price of the house

script.Parent.Touched:connect(function(part)
    if part.Parent:FindFirstChild("Humanoid") then
        if game.Players:FindFirstChild(part.Parent.Name).leaderstats["Cash"].Value >= houseprice then-- Cash is my currency. Change it to your currency name.
            if not game.Workspace:FindFirstChild(part.Parent.Name.."'s House") then -- Finds if the player has an existing house.
                if script.Parent.Parent.Parent.Owner.Value == "" then -- Make sure this house has no owner.
                    game.Players:FindFirstChild(part.Parent.Name).leaderstats["Cash"].Value = game.Players:FindFirstChild(part.Parent.Name).leaderstats["Cash"].Value - houseprice * 1 -- Deducts the cost of the house to the player's cash
                    script.Parent.Parent.Parent.Owner.Value = part.Parent.Name -- We change the "Owner" stringvalue to the name of the player who bought the house.
                    script.Parent.Parent.Name = part.Parent.Name.."'s House" -- Change the "Buy this house!" to the name of the owner
                    script.Parent.Parent.Parent.Name = part.Parent.Name.."'s House" -- Change the house name to the owner's name to make it easy to find when the player left
                end
            end
        end
    end
end)
0
(thank you JesseSong for making the code easier to read, I'm new to this site) kitty577421 0 — 4y
0
Your code is fine, I think it might be a problem with your cash debug. Optikk 499 — 4y
0
I'll try to mess around with the script, then kitty577421 0 — 4y

2 answers

Log in to vote
0
Answered by
marfit 104
4 years ago
Edited 4 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Instead of doing

game.Players:FindFirstChild(part.Parent.Name).leaderstats["Cash"].Value = game.Players:FindFirstChild(part.Parent.Name).leaderstats["Cash"].Value - houseprice * 1

You should use the new Luau syntax -= which sets that object to itself minus the argument afterwards, as so:

local A = 5
local B = 2

A -= B
print(A)--3
A = A - B
print(A) -- 3 as well, assuming you only have one or the other

Your code would be:

game.Players:FindFirstChild(part.Parent.Name).leaderstats["Cash"].Value -= houseprice * 1
0
Thanks! kitty577421 0 — 4y
0
It still sets it to 500 kitty577421 0 — 4y
0
Then it is not this code, but something else marfit 104 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

You may have a duplicate script.

Answer this question