Leaderstats Awarding More Points Than Intended

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

Add a debounce to make sure it only runs once.

5 Likes

That also happened to me at some point, you have to realize that the player will touch the face multiple times. Use something like debounce.

1 Like
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)

While this is a good script, debounce should be checked activated where the if (score~=nil) part.

Thanks for suggesting it, I implied it :slight_smile: .

You can use if thisValue.Value == true and not debounce then instead of creating a whole new if statement

Well, it works too but doing it is depended on the scripter :slight_smile: .

Using your script creates conflict with a couple of lines.

if not debounce then

This creates an issue with “debounce”.

score.Value = score.Value + amnt

This creates an issue with “amnt”.

I must also add that using this script just flat out doesn’t work at all. The points don’t change.

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)
1 Like

Thank you! This one seemed to work just fine. I’d also like to add that I DID in fact credit you with this script.