Hello DevForum users! I’ve got a minor but somewhat major problem in my game Ride a Box Into Shedletsky’s Face. The goal is to spawn in a box and fly through Shedletsky’s face. When you touch his face you get 1 Face Pwn. This number does not multiply, it is always 1 each time. My issue is that when you run through it, you get anywhere from 3-6 times the amount you’re supposed to get.
Here is my code:
amnt = 1
function ontouched(part)
local h = part.Parent:findFirstChild("Humanoid")
if(h~=nil) then
local thisplr = game.Players:FindFirstChild(h.Parent.Name)
if(thisplr~=nil) then
local stats = thisplr:findFirstChild("leaderstats")
if(stats~=nil) then
local score = stats:findFirstChild("Face Pwns")
if(score~=nil) then
wait (1)
score.Value = score.Value + amnt
end
end
end
end
end
script.Parent.Touched:connect(ontouched)
I genuinely could not find a solution to this problem ANYWHERE. That’s why I’m here, at the core of developing intelligence.
Video Example: rbxstdtest1 - YouTube
amnt = 1
local debounce = false
function ontouched(part)
local h = part.Parent:findFirstChild("Humanoid")
if(h~=nil) then
local thisplr = game.Players:FindFirstChild(h.Parent.Name)
if(thisplr~=nil) then
local stats = thisplr:findFirstChild("leaderstats")
if(stats~=nil) then
local score = stats:findFirstChild("Face Pwns")
if(score~=nil) then
if not debounce then
debounce = true
wait(1)
score.Value = score.Value + amnt
wait(2)
debounce = false
end
end
end
end
end
end
script.Parent.Touched:connect(ontouched)
So what I would suggest is firstly using the += function, and making the code a bit more readable and maybe using a remote event to change player’s leaderstats but here is a improved script that I didn’t test but maybe it can help you out.
-- Handler.lua
-- DaffyDavinko
-- May 22, 2021
--// Constants
local _Amount = 1
local DB = false
local DebounceWait = 1.2
--// Variables
local Players = game:GetService("Players")
local Part = script.Parent
function OnTouched(part)
local Humanoid = part.Parent:findFirstChild("Humanoid")
if Humanoid ~= nil or {} then
local TargetPlayer = game.Players:FindFirstChild(Humanoid.Parent.Name)
if TargetPlayer ~= nil then
local Leaderstats = TargetPlayer:FindFirstChild("leaderstats")
if stats~=nil or {} then
local Pwns = Leaderstats["Face Pwns"]
if Pwns ~=nil then
if not DB then
DB = true
wait(DebounceWait)
Pwns.Value += _Amount
wait(DebounceWait)
DB = false
print("Amount Gave: ".._Amount)
end
end
end
end
end
end
--// Connections
Part.Touched:Connect(OnTouched)