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

If A Play Touches A Block, A Block Becomes CanCollide, If A Player Gets Off It Is Not CanCollide?

I am trying to make it so if you touch a block a block become cancollide, if you get off it uncancollides. I have NPCs too, and they trigger the code and I don't want them to trigger it. I've been trying to do this and I can't find anything on google or youtube that will help me. Please help.

Thank You!

(My Code)

local block1 = game.Workspace.FrontBlock1
local block2 = game.Workspace.FrontBlock2
local block3 = game.Workspace.BackBlock1
local block4 = game.Workspace.BackBlock2

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
	wait(0.5)
	block1.CanCollide = true
	block2.CanCollide = true
	block3.CanCollide = true
	block4.CanCollide = true
else
	block1.CanCollide = false
	block2.CanCollide = false
	block3.CanCollide = false
	block4.CanCollide = false
	end
end)

1 answer

✓ Accepted answer

You could make the npcs health and max health to 101: and do this:

local block1 = game.Workspace.FrontBlock1
local block2 = game.Workspace.FrontBlock2
local block3 = game.Workspace.BackBlock1
local block4 = game.Workspace.BackBlock2

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    local humanoid = player:FindFirstChild("Humanoid")

    if player and humanoid.Health == 100 then -- detects if the player health is 100, or you can set the health to your game's default, thats means npcs with 101 health could not access to the brick
    wait(0.5)
    block1.CanCollide = true
    block2.CanCollide = true
    block3.CanCollide = true
    block4.CanCollide = true
else
    block1.CanCollide = false
    block2.CanCollide = false
    block3.CanCollide = false
    block4.CanCollide = false
    end
end)

1 comment

0

I believe there is more complicated and useful methods, but I hope this helped! Thanks for reading!

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