#Safe zone question

1 messages · Page 1 of 1 (latest)

mighty hawk
#

So i'm making a wave defense game based on piggy characters and the round mechanic works perfectly but i'm trying a safe zone for the lobby so when the players are in the lobby the rounds won't start until they get out. I made a part that is around the entire lobby and named it SafePart but i'm new to studio so idk how to script it so the guis and the spawners won't work until the player is out of the SafePart. (The model for the wave defense is My Wave Defense Kit(pls don't steal) by jaydonashford and it was made in the 18/12/2024 at 11pm. thx for helping it will help me to learn scripting a lot and save me a lot of time 😄

hard elbow
#

Part.Touched and Part.TouchEnded

hollow sinewBOT
#

studio** You are now Level 1! **studio

hard elbow
#

When touched you enable the safezone effect and TouchEnded you disable it

#

part.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
end)

ornate hazel
#

TouchEnded fires when the players stand still tho

#

I think

mighty hawk
#

K i will try it

mighty hawk
#

Like if the player is in the safe zone i disable the round script ?

ornate hazel
#

Might not be an efficient way to do a safe zone, but it works

ornate hazel
#

Like if you want to prevent someone from damaging another in Safe Zone, you can check if the player is in/out of a Safe Zone before applying the damage.

#

Script I made quickly in ServerScriptService:

local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local SafeZoneList = CollectionService:GetTagged("SafeZone")
local SafeZoneData = {}

for _, ZonePart: Part in SafeZoneList do
    SafeZoneData[ZonePart] = {Players = {}, Loops = {}}
    
    ZonePart.Touched:Connect(function(TouchedPart)
        local Character = TouchedPart:FindFirstAncestorOfClass("Model")
        local Player = Character and Players:GetPlayerFromCharacter(Character)
        
        if Player ~= nil and not SafeZoneData[ZonePart].Players[Player.UserId] then
            Player:AddTag("InSafeZone")
            
            SafeZoneData[ZonePart].Players[Player.UserId] = true
            SafeZoneData[ZonePart].Loops[Player.UserId] = RunService.Heartbeat:Connect(function()
                local HitList = workspace:GetPartBoundsInBox(ZonePart.CFrame, ZonePart.Size)
                
                if Character and not table.find(HitList, Character.PrimaryPart) then
                    SafeZoneData[ZonePart].Players[Player.UserId] = nil
                    Player:RemoveTag("InSafeZone")
                    SafeZoneData[ZonePart].Loops[Player.UserId]:Disconnect()
                end
            end)
        end
    end)
end
#

You'll have to set-up a BasePart with a tag named "SafeZone"

#

@mighty hawk

hard elbow
#

SetProperty for the player for that no need fot exsessive tables n other

ornate hazel
#

I need to use a table to store the RBXScriptConnection

hard elbow
#

Player:SetProperty("InSafeZone", true/false)

#

Player:GetProperty()

ornate hazel
#

This function doesn't exists?

#

I know about :SetAttribute and :AddTag

#

But not :SetProperty

hard elbow
#

Oh wait

#

Attribute

#

Yea

ornate hazel
#

That doesn't change anything, using Tag is same

hard elbow
#

I messed it up with another thingy mb

ornate hazel
#

Np

hard elbow
#

I mean

#

This is a loop that runs every frame no?

#

Did touchEnded not work?

ornate hazel
ornate hazel
hard elbow
#

Idk the docs said whenever the colision stops with the part it fires

#

Since roblox has a check for that

#

I'd test it if i was at my computer rn but cant

mighty hawk
mighty hawk
turbid hemlock
#
local InSafezone = false

safezone.Touched:Connect(function(hit)
    if not InSafezone then
        InSafezone = true
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        print("In safezone!")
    end
end)

safezone.TouchEnded:Connect(function(hit)
    if InSafezone then
        InSafezone = false
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        print("Out of safezone!")
    end
end)```
#

done

#

it works i tested it

#

just slap in your tag and chech it instead of the variable

#

check*

mighty hawk
#

Or is it like for people to not hurt each other

turbid hemlock
#

it works with anything

#

just add the tag

mighty hawk
turbid hemlock
#

Player:RemoveTag("InSafeZone") and Player:AddTag

mighty hawk
#

K

mighty hawk
#

In the thing i want to work with right?

#

I'm gonna try thx for helping everyone

turbid hemlock
#

safezone.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player and not player:HasTag("InSafeZone") then
        player:AddTag("InSafeZone")
        print("In safezone!")
    end
end)

safezone.TouchEnded:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player and player:HasTag("InSafeZone") then
        player:RemoveTag("InSafeZone")
        print("Out of safezone!")
    end
end)```
#

this

#

it's a server script under the safezone part btw

mighty hawk
#

K

ornate hazel
#

Wow TouchEnded actually works, I always thought it wasn't working

mighty hawk
#

Wdym

#

What works with TouchEnded?

ornate hazel
#

Games that uses Touched seemed to not working properly like the lava in BloxFruits

#

But ig they just don't use TouchEnded

mighty hawk
#

Oh ok

ornate hazel
#

Also if you want the SafeZone to work even in wierd positions like this...

mighty hawk
#

Yeah?

ornate hazel
#

...you'll need to check if the "hit" part is the HumanoidRootPart

ornate hazel
#

It works without

mighty hawk
#

Ki doki

#

Thx for helping

ornate hazel
#

Thanks to Veltrixion

mighty hawk
#

I will try what y'all said now

#

I would come back if i have any problem

ornate hazel
#

K

#

👋

mighty hawk
#

👋

turbid hemlock
#

dude it works with any pos

#

the same script i provided

#

works with any part and is accurate instead of using just the humanoid root part

#

safezone.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player and not player:HasTag("InSafeZone") then
        player:AddTag("InSafeZone")
        print("In safezone!")
    end
end)

safezone.TouchEnded:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player and player:HasTag("InSafeZone") then
        player:RemoveTag("InSafeZone")
        print("Out of safezone!")
    end
end)```
ornate hazel
#

If you have something like a breathing animation, since the parts are all effecting the Tag, if a limb goes outside of the zone, the Tag will be removed even that the player is still in.

turbid hemlock
#

oh wait yeah i just did some spinning around it bugs out

#

but yeah use the humanoidrootpart