local debounce = false function onTouch(hit) if hit == nil then return end if hit.Parent == nil then return end if not game.Workspace:FindFirstChild("Home"..hit.Parent.Name) then if game.Players:findFirstChild(hit.Parent.Name) ~= nil and debounce == false then debounce = true local b = script.Parent.Parent:Clone() b.Parent = game.Lighting b.Name = "Home"..hit.Parent.Name wait(.4) script.Parent.Parent.OwnerName.Value = hit.Parent.Name script.Parent.Head:Remove() script.Parent.Parent["Home Owner: Nobody"].Name = "Home Owner: "..hit.Parent.Name script.Parent.Parent.Parent.Name = "Home"..hit.Parent.Name end else end wait(3) debounce = false end script.Parent.Head.Touched:connect(onTouch) Thanks, Fersist
Essentially you want to do something like this:
local cash = player.leaderstats.Cash if cash.Value < 100 then return end -- player doesn't have enough money cash.Value = cash.Value - 100
If you wish to make it so that it costs money for someone to buy a home, your script could look like this:
local debounce = false local cost = 100 function onTouch(hit) if not hit.Parent or debounce then return end local player = game.Players:PlayerFromCharacter(hit.Parent) if not player then return end local cash = player.leaderstats.Cash if cash.Value < cost then return end -- player doesn't have enough money cash.Value = cash.Value - cost debounce = true local b = script.Parent.Parent:Clone() b.Parent = game.Lighting b.Name = "Home"..player.Name wait(.4) script.Parent.Parent.OwnerName.Value = hit.Parent.Name script.Parent.Head:Destroy() script.Parent.Parent["Home Owner: Nobody"].Name = "Home Owner: "..hit.Parent.Name script.Parent.Parent.Parent.Name = "Home"..hit.Parent.Name wait(3) debounce = false end script.Parent.Head.Touched:connect(onTouch)
Note, I made the following unnecessary changes:
if hit == nil then return end
because hit
can never be nilif
statements and made it so that if any condition isn't met, the function returns earlyFindFirstChild
, not findFirstChild
; use Destroy()
not Remove()
-- alternatively you can use .Parent = nil
if you want to re-use it later).Instead of debounce = false
, you might consider just removing the script (with script:Destroy()
), since once you've removed the Head, this script can't do anything else.
Also note that the player will lose their money if the script fails (it shouldn't if you have everything set up correctly and if no other scripts might interfere). You could reduce this risk by taking out the wait(.4)
and moving the cash reduction to right before the wait(3)
currently is.