Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I want to change this script up but how?

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?

1 comment

0

By the way, I got this Code from someone here on Scripting helpers. That's why there are Instructions.

1 answer

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!

2 comments

0

It works for the most part but now sometimes I don't get any points at all.

0

That is because of the debounce, you can change how much time it waits before it allows someone to touch it again. Just change whats inside the wait function.

This discussion is preserved as it was. New answers, comments, and votes are closed.