Hello developpers, I want to make my monster chasing only if player is in certain area. Can someone help me to do it because I don’t know how to make it : (
This can help. It’s exactly what you want I think:
I don’t understand . I’ve made a value, when player touching the part (area) the value turn true but when the player leave the area, the value is false. But the value turn true and false when the player is in the area. I use touched and touchedended event. I don’t know : (
Try using workspace:GetPartsInBounds
Where do I put it ?
There is the script :
local Players = game:GetService("Players")
local Guardian = game.Workspace.Guardians.Guardian2
local Part = game.Workspace.Guardian2Part
Part.Touched:Connect(function(Hit)
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
Guardian.CanChase.Value = true
else
Guardian.CanChase.Value = false
end
end)
Part.TouchEnded:Connect(function(Hit)
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
if Player then
Guardian.CanChase.Value = false
else
Guardian.CanChase.Value = false
end
end)
When doing that, my favorite option is to create a raycast from the character’s rootpart and angle it down to detect any parts the player is currently standing on
how can i do it ? (30 letters)
Im not on my computer rn so I can’t type it for you, but it’s very simple, just send a ray down the character’s rootpart and make it detect any safezones. Whitelisting only the parts the monster won’t chase you
What I would personally do is make it so that when the player is inside an area ( make like a massive part for it and transparency 1 and cantouch is false and stuff), and make it so the monster guy cannot move when the character isn’t touching and it can when it does, or it could despawn or something, your choice
Try this:
local region = Region3.new(Vector3.new(5, 0, 5), Vector3.new(15,15, 15))
local part2 = Instance.new("Part", game.Workspace)
part2.Anchored = true
part2.Name = "Region3"
part2.Size = region.Size
part2.CanCollide = false
part2.BrickColor = BrickColor.new("Really black")
part2.CFrame = region.CFrame
while true do
task.wait()
local partsInRegion = game.Workspace:FindPartsInRegion3(region)
for i, v in pairs(partsInRegion) do
if v.Parent:FindFirstChild("Humanoid") then
if v.Parent:FindFirstChild("DoNotKill") then
print("Player already has value.") -- Make sure to remove this line as it would cause unneccesary lag, I wrote it just for testing purposes.
else
local newValue = Instance.new("BoolValue")
newValue.Name = "DoNotKill"
newValue.Parent = v.Parent
end
end
end
end
There is probably a better way to do this, but this works for me.
Explanation: What this code does is that it creates an “imaginary” area, and it creates a part that’s in the same size and position as that area. And then there is a loop that is constantly checking for parts in that imaginary area, if it finds a humanoid, which means it is a player, it adds a value called “DoNotKill” in the player’s character. By the way, you don’t need the part, just the area itself. I only created the part so you can visualize it.
can you make it so it checks every so often to see if the humanoid is still there?