Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

I Don't Know Why This Script Repeats Twice?

Asked by 6 years ago
Edited 6 years ago
script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local object = player.Character
local ls = player.leaderstats
local sl = game.Workspace:FindFirstChild(ls.Stage.Value)
object.Torso.CFrame = sl.CFrame + Vector3.new(0,3,0)
print(num) --shows that it repeats twice
num = num + 1
end)
1
add a debounce, I noticed if I add a print, and touch the part, it prints over 100 times in 3 seconds. greatneil80 2647 — 6y

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
6 years ago

The Event you’re Connecting too is causing this issue, .Touched can fire multiple times in a few seconds, as simply moving will reenable the function once again, easy fix though, you just need to add a debounce, it would look somewhat like this compared to your Script.

local part = script.Parent
local touched = false
part.Touched:Connect(function(hit)
   if touched then return end
   if (hit) ~= nil) then
      touched = true
      local player = game.Players:GetPlayerFromCharacter(hit.Parent)
      local character = player.Character or player.CharacterAdded:wait()
      local leader = player.leaderstats
      local stage = workspace:FindFirstChild(leader.Stage.Value)
      Character:MoveTo(stage.CFrame).p + Vector3.new(0,3,0)
      repeat
      local parts part:GetTouchingParts()
      if (parts == nil) then
         touching = false
      end
      until not touching
   end
end)

Hope this works out for you, I also switched up a few things for efficiency, if so don’t forget to accept the answer and upvote! Good luck!

Ad

Answer this question