Hello, I would like to know what I am doing wrong, I need to make that when the part is touched it gains +10 money and then the part is destroyed, how do I do that?
local Player = game:GetService("Players")
local debounce = false
script.Parent.Touched:Connect(function(hit)
local PlayerCharacter = Player:GetPlayerFromCharacter(hit.Parent)
if PlayerCharacter then
debounce = false
local Leaderstats = PlayerCharacter:WaitForChild("leaderstats")
Leaderstats.Cash.Value = Leaderstats.Cash.Value + 10
debounce = true
wait(3)
script.Parent:Destroy()
end
end)
You aren’t checking for the debounce at all. You are just changing it. Additionally, the debounce will be useless if it isn’t reverted to true after your wait. Though since you want to Destroy the part anyways after a single hit, a debounce is not necessary. Just disconnect the function once it’s activated.
Yeah, just disconnecting it so it doesn’t fire again should work.
local Player = game:GetService("Players")
local connection
connection = script.Parent.Touched:Connect(function(hit)
local PlayerCharacter = Player:GetPlayerFromCharacter(hit.Parent)
if PlayerCharacter then
connection:Disconnect() --Prevents the function from being fired again through script.Parent.Touched
local Leaderstats = PlayerCharacter:WaitForChild("leaderstats")
Leaderstats.Cash.Value = Leaderstats.Cash.Value + 10
wait(3)
script.Parent:Destroy()
end
end)