I got a coin system thats super bugged out, it only works 75% of the time and i need HELP
I cant find whats wrong here
Basically when the player touches the coin confetti comes up with a sound and updates the leaderstat value, Problem is 25% of the time the coin script doesn’t work and when i touch it nothing updates orhappens
----- | Services | -----
local Players = game:GetService("Players")
----- | Variables | -----
WaitTime = 300
Amount = 75
Debounce = false
----- | Method | -----
function onTouch(part)
if not Debounce then Debounce = true
local Human = part.Parent:FindFirstChild("Humanoid")
if Human then
local Player = Players:GetPlayerFromCharacter(Human.Parent)
if Player then
local leaderStat = Player:FindFirstChild("leaderstats") :FindFirstChild("EggCoins")
if leaderStat then
leaderStat.Value += Amount
end
end
script.Parent.Music:Play()
script.Parent.Day.Enabled = true
script.Parent.Transparency = 1
task.wait(1.25)
script.Parent.Day.Enabled = false
script.Disabled = true
task.wait(WaitTime)
script.Parent.Transparency = 0
script.Disabled = false
Debounce = false
end
end
end
----- | Callback | -----
script.Parent.Touched:Connect(onTouch)
I was just thinking the same thing about printing.
I added a bunch of print statements to your script:
----- | Services | -----
local Players = game:GetService("Players")
----- | Variables | -----
WaitTime = 300
Amount = 75
Debounce = false
----- | Method | -----
function onTouch(part)
print("Touched")
if not Debounce then Debounce = true
local Human = part.Parent:FindFirstChild("Humanoid")
if Human then
local Player = Players:GetPlayerFromCharacter(Human.Parent)
if Player then
local leaderStat = Player:FindFirstChild("leaderstats") :FindFirstChild("EggCoins")
if leaderStat then
leaderStat.Value += Amount
else
warn("No Leaderstat")
end
else
warn("No Player")
end
script.Parent.Music:Play()
script.Parent.Day.Enabled = true
script.Parent.Transparency = 1
task.wait(1.25)
script.Parent.Day.Enabled = false
script.Disabled = true
task.wait(WaitTime)
script.Parent.Transparency = 0
script.Disabled = false
Debounce = false
else
warn("No Human")
end
else
warn("Wait")
end
end
----- | Callback | -----
script.Parent.Touched:Connect(onTouch)
----- | Services | -----
local Players = game:GetService("Players")
----- | Variables | -----
WaitTime = 300
Amount = 75
Debounce = false
----- | Method | -----
function onTouch(part)
if not Debounce then Debounce = true
print("bounced")
local Human = part.Parent:FindFirstChild("Humanoid")
if Human then
print("human")
local Player = Players:GetPlayerFromCharacter(Human.Parent)
if Player then
print('plr')
local leaderStat = Player:FindFirstChild("leaderstats") :FindFirstChild("EggCoins")
if leaderStat then
print('leader')
leaderStat.Value += Amount
end
end
print('extrastuff')
script.Parent.Music:Play()
script.Parent.Day.Enabled = true
script.Parent.Transparency = 1
task.wait(1.25)
script.Parent.Day.Enabled = false
script.Disabled = true
task.wait(WaitTime)
script.Parent.Transparency = 0
script.Disabled = false
Debounce = false
print('works')
end
end
end
----- | Callback | -----
script.Parent.Touched:Connect(onTouch)
Your debounce is only setting back again if the coin touched a human. If it touched anyth else before, it will get locked with no way to pass debounce again.
----- | Services | -----
local Players = game:GetService("Players")
----- | Variables | -----
WaitTime = 300
Amount = 75
Debounce = false
----- | Method | -----
function onTouch(part)
print('touched')
print("bounced")
local Human = part.Parent:FindFirstChild("Humanoid")
if Human then
print("human")
local Player = Players:GetPlayerFromCharacter(Human.Parent)
if not Debounce then Debounce = true
if Player then
print('plr')
local leaderStat = Player:FindFirstChild("leaderstats") :FindFirstChild("EggCoins")
if leaderStat then
print('leader')
leaderStat.Value += Amount
end
end
print('extrastuff')
script.Parent.Music:Play()
script.Parent.Day.Enabled = true
script.Parent.Transparency = 1
task.wait(1.25)
script.Parent.Day.Enabled = false
script.Disabled = true
task.wait(WaitTime)
script.Parent.Transparency = 0
script.Disabled = false
Debounce = false
print('works')
end
end
end
----- | Callback | -----
script.Parent.Touched:Connect(onTouch)
local Debounce = false
function onTouch(part)
if Debounce then return end
Debounce = true
task.delay(1, function() -- 1 second
Debounce = false
end)
-- code code code
end
You need to make sure there is a humanoid before assigning it to a variable.
Also, it is useful to build warnings into your code instead of print statements.
That way they only show up in the Output window when there is a problem.
Instead of blowing up your Output window with print statements.
Try this:
----- | Services | -----
local Players = game:GetService("Players")
----- | Variables | -----
WaitTime = 300
Amount = 75
Debounce = false
----- | Method | -----
function onTouch(part)
print("Touched")
if part.Parent:FindFirstChild("Humanoid") then
local Human = part.Parent.Humanoid
if not Debounce then Debounce = true
if Human then
local Player = Players:GetPlayerFromCharacter(Human.Parent)
if Player then
local leaderStat = Player:FindFirstChild("leaderstats") :FindFirstChild("EggCoins")
if leaderStat then
leaderStat.Value += Amount
else
warn("No Leaderstat")
end
else
warn("No Player")
end
script.Parent.Music:Play()
script.Parent.Day.Enabled = true
script.Parent.Transparency = 1
task.wait(1.25)
script.Parent.Day.Enabled = false
script.Disabled = true
task.wait(WaitTime)
script.Parent.Transparency = 0
script.Disabled = false
else
warn("No Human")
end
else
warn("Wait")
end
Debounce = false
end
end
----- | Callback | -----
script.Parent.Touched:Connect(onTouch)
Ah, my bad, i havent even read the rest of the code to understand the context.
Sinse the coin is disapearing and reapearing after task.wait, you can set the debounce false and true there too.
Remove the Debounce setting true from the beginning and put it together with the part where the coin goes transparent, and set back at the part the transparency is retored.