Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make it so that this script will deduct cash or the leaderstat?

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

1 answer

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:

  • I took out if hit == nil then return end because hit can never be nil
  • I merged some of the if statements and made it so that if any condition isn't met, the function returns early
  • Some of the code was checking to see if the player had a character in the workspace. This isn't valuable, so I took it out.
  • I updated deprecated functions (ex, use FindFirstChild, 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.

This discussion is preserved as it was. New answers, comments, and votes are closed.