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

I want to change this script up but how?

Asked by 10 years ago

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?

0
By the way, I got this Code from someone here on Scripting helpers. That's why there are Instructions. CrispyBrix 113 — 10y

1 answer

Log in to vote
1
Answered by
Gamenew09 180
10 years ago

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!

0
It works for the most part but now sometimes I don't get any points at all. CrispyBrix 113 — 10y
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. Gamenew09 180 — 10y
Ad

Answer this question