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

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

Asked by 3 years ago

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

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

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)
0
I believe there is more complicated and useful methods, but I hope this helped! Thanks for reading! Xapelize 2658 — 3y
Ad

Answer this question