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

How do I make the last block that the charcter used to trigger the code?

Asked by 5 years ago

I made a button that spawns a block "SpawnRedBlock" the spawned block should then be put in another block with the same color "RecycleBinRed". It will make the SpawnRedBlock to fall throught the "RedRecycleBin" (and the ground at the moment)

I also done a green and blue button and blocks that works the same way and you cant put the wrong color in the wrong "Bin"

The problem I have is if I spawn more then one block of the same color and putt the second in the bin. Then the first block will fall through the ground insted of the one colliding with the RecycleBin. The "SpawnBlock" will continue to do this in the order they were spawned

I want the last block that the charcter used to trigger the code.

But I am new to programing and lua so if anyone would help me it would be nice to also explain why you used to code as you did.

the script I put in the button

local clickdetector = script.Parent:WaitForChild("ClickDetector")

clickdetector.MouseClick:connect(function(player)

    local part = Instance.new("Tool", workspace)
    part.Name = "SpawnRedBlock"

        local handle = Instance.new("Part",part)
        handle.Name = "Handle"
        handle.BrickColor = BrickColor.Red()

        local BrickCD = Instance.new("ClickDetector", part)
        BrickCD.Name = "RedBrickCD"

        BrickCD.MouseClick:connect(function(player)

        print("Red Brick")

    end)


end)

The script I put in the RedRecycleBin

script.Parent.Touched:Connect(function(hit)
        if hit.Parent.Name == "SpawnRedBlock"
         then
            game.workspace.SpawnRedBlock.Handle.CanCollide = false
            script.Parent.Transparency = 0.5
            wait(2)
            script.Parent.CanCollide = true
            script.Parent.Transparency = 0
        end
    end) 
0
don't use the parent argument of instance.new, that can have adverse impacts on your game's performance theking48989987 2147 — 5y
0
Tank you for answering even thought I dont understand exatly what you mean. In what way will it impact the performence? and is there an alternetive way to create the parts without putting them in the game from the start? TheRealBrynolf 0 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

A friend helped me solve this problem so I wanted to show what I did wrong. I left the old text as comment just so one can see de misstake I did.

script.Parent.Touched:Connect(function(hit)
        if hit.Parent.Name == "SpawnRedBlock"
         then
            -- game.workspace.SpawnRedBlock.Handle.CanCollide = false
            hit.Parent.Handle.CanCollide = false
            script.Parent.Transparency = 0.5
            wait(2)
            script.Parent.CanCollide = true
            script.Parent.Transparency = 0
        end
    end) 
Ad

Answer this question