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

Why won't this subtract money when clicked?

location = script.Parent.Parent.Parent
regen = script.Parent.Parent
save = regen:clone()

function onClicked()
    back = save:clone()
    wait(1)
    back.Parent = location
    back:MakeJoints()
end
wait(15)

function onClicked()
    back = save:clone()
    wait(1)
    back.Parent = location
    back:MakeJoints()
end
 
function onClicked(hit)
 if hit.Parent.Humanoid~= nil then
  cash = game.Players:FindFirstChild(hit.Parent.Name).leaderstats.Cash -- Find out who touched.
  if cash.Value >= 100 then
   cash.Value = cash.Value - 100
  end
 end
end
script.Parent.Touched:connect(onClicked)

This script is supposed to make a car respawn, and cost money when clicked on the button. Worked perfectly fine when respawning, but still doesn't take money away from leaderboard, what am I doing wrong? Please could you put answer in script itself... I have a hard time understanding it otherwise.

1 comment

0

Any errors in output? Add prints in each line to see if the code stops somewhere.

1 answer

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

Your problem was that you weren't getting the Player properly. Using :GetPlayerFromCharacter will get the player who touched it. So if we put that in, it should look like this:

function onClicked(hit)
	if hit.Parent.Humanoid~= nil then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		cash = player:WaitForChild("leaderstats").Cash
		if cash.Value >= 100 then
			cash.Value = cash.Value - 100
		end
	end
end

script.Parent.Touched:connect(onClicked)

Another problem it might be having is that it works, but it's not working how you want it to. After it gets the Cash, it checks to see if it is >= 100, but if the players cash.Value isn't >= 100, then the code will still regenerate the object, and not take away the Cash. To fix this, we want to check to make sure the player has cash.Value >= 100 before any other part of the script. To do this, we just put this above the regenerate.

This should be the look of the final, working script:

location = script.Parent.Parent.Parent
regen = script.Parent.Parent
save = regen:clone()

wait(15)

function onClicked(hit)
	if hit.Parent.Humanoid~= nil then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		cash = player:WaitForChild("leaderstats").Cash
		if cash.Value >= 100 then
			cash.Value = cash.Value - 100
			back = save:clone()
   			wait(1)
   			back.Parent = location
			back:MakeJoints()
		else
			print("Not enough Cash") -- Just tells you that they don't have enough Cash
		end
	end
end

script.Parent.Touched:connect(onClicked)

One last thing I didn't quite understand, is why you had 3 different onClicked functions. You could just chuck them all into the one function, and it would work. You also had 2 of the same function, so I just removed one of them, and put the other inside the function.

It should now work. If it doesn't, please reply back with any errors in the output, and I'll go back and see what is wrong.

If it does work, then be sure to +1 and accept answer.

  • NinjoOnline

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