I have this Script that is inside a Part called "TeleBrick" This script works fine but it is causeing problems and I know why I think it is beuase the Part when it is CFreamed it moves into player adding extra Points. So I want to make it so that when it is Touched, it Destroys and then Adds another part to the Workspace the exact same. Heres the Code:
local Brick = game.Workspace.TeleBrick --Replace this with your brick Brick.Touched:connect(function(Obj) --Connects the Touched event to an anonymous function Brick.Transparency = 1 Brick.CFrame = CFrame.new( --Replace the Min and Max in the math.random() with your numbers math.random(-62.5, 62.5 ), math.random(3.19, 3.19), math.random(-62.5, 62.5) ) Brick.Transparency = 0 if Obj and Obj.Parent then --Making sure the Obj still exists local Player = game.Players:GetPlayerFromCharacter(Obj.Parent) --Gets the player from the character if Player then local PointStat = Player.leaderstats:FindFirstChild("Points") --Replace STAT_NAME with the name of the stat if PointStat then PointStat.Value = PointStat.Value + 2 --Change 1 to how many points you want to award end end end end)
Can anyone help me out here?
What you need to do is add Debounce. Debounce prevents a piece of code from running too many times.
local Brick = game.Workspace.TeleBrick --Replace this with your brick local debounce = false Brick.Touched:connect(function(Obj) --Connects the Touched event to an anonymous function if debounce == true then return end -- Prevents the code from executing multiple times. debounce = true -- Makes the above line work. Brick.Transparency = 1 Brick.CFrame = CFrame.new( --Replace the Min and Max in the math.random() with your numbers math.random(-62.5, 62.5 ), math.random(3.19, 3.19), math.random(-62.5, 62.5) ) Brick.Transparency = 0 if Obj and Obj.Parent then --Making sure the Obj still exists local Player = game.Players:GetPlayerFromCharacter(Obj.Parent) --Gets the player from the character if Player then local PointStat = Player.leaderstats:FindFirstChild("Points") --Replace STAT_NAME with the name of the stat if PointStat then PointStat.Value = PointStat.Value + 2 --Change 1 to how many points you want to award end end end wait(1) -- Waits a second before the code below executes. debounce = false -- Makes the code work once again. end)
If I helped please put me as the answer and vote me up. Thank you for reading!