1
votes

I need a bit of help. Basically I have this code:

local plyIsEntered = false

function onTouched(hit)

plyIsEntered = true

if not plyIsEntered then

end

if plyIsEntered then

    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    local ply = humanoid.Parent
    if humanoid ~= nil then

        print("Hit")
        local playerName = hit.Parent.Name
        print(playerName)
        local laserEmitter = game.Workspace["Enterance PC"]:FindFirstChild("laserEmitter")

        local scanLaser = Instance.new("Part", game.Workspace)

        scanLaser.Position = laserEmitter.Position
        scanLaser.Name = "ScanLaser"
        scanLaser.Size = Vector3.new(1,1,1)
        local scanLaserMesh = Instance.new("SpecialMesh", game.Workspace.ScanLaser)
        scanLaserMesh.Name = "Cone mesh"
        scanLaserMesh.MeshType = ""
        plyIsEntered = false


        end

    end



end

script.Parent.Touched:connect(onTouched)

Now I'm checking if the player touches a box, it has no collisions and is invisible; when they do I want to create a laser that will scan them and open a door. The problem I'm having is when I walk into the trigger box it creates 8 or 9 blocks. One of those blocks is the block I'm applying a mesh too.

What I need to do is make sure it's only running once and not creating more than 1 brick. Hopefully someone can help me!

1
For sone strange reason it won't let me edit... You need to fix code formatting. Also don't forget roblox has it's own Scripters forum who help with these things. Advice aside, .Touched fires A LOT, so you should look at debounce. wiki.roblox.com/index.php?title=Debouncewarspyking

1 Answers

3
votes

I believe to fix this you'll need to add a debounce.

You see, the touched event of a Part actually fires many times, so your code will execute multiple times if it is inside of the event.

To fix this, we use a debounce, which means your code won't execute if your part is touched too much in the same time frame. Here is an example:

local debounce = false

part.Touched:connect(function()
   if debounce == false then
     debounce = true
     --Your code goes here.
     wait(1)--Wait one second until you'll be able to execute the code again.
     debounce = false
   end
end)

To read more on debounces: http://wiki.roblox.com/index.php?title=Debounce