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

Is there a more reliable way of using Touch (or anything familiar)?

Is anyone able to find a way to make this more reliable?

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
        plr.Protected.Value = true
        
        
        script.Parent.TouchEnded:Connect(function(hit)
        plr.Protected.Value = false
        end)
    end
    
end)

Basically a part is a protection zone and if the player enters it, they won't be affected by an explosion. When they leave however they will. Sometimes however the player still gets affected even though they are inside the force field

2 comments

0

Region3

0

Don't define your TouchEnded inside the Touched event's function. Definie it independent of Touched.

1 answer

Hey RjsMc

We can use Region3 for this :)

local safeZonePart = workspace:WaitForChild("SafeZonePart")
local starterUI = game:GetService("StarterGui")
local player = game:GetService("Players").LocalPlayer
local playerUI = player:WaitForChild("PlayerGui")
local char = player.Character or player.CharacterAdded:Wait()
local pos1 = (safeZonePart.Position - (safeZonePart.Size / 2))
local pos2 = (safeZonePart.Position + (safeZonePart.Size / 2))

local newRegion = Region3.new(pos1, pos2)

local deb = false

while true do
wait(.5)
local plrsInReg = workspace:FindPartsInRegion3(newRegion, workspace.Terrain)

for i,v in pairs(plrsInReg) do
	if v:FindFirstAncestor(player.Name) then
			
	if not deb then
		deb = true
		end
	else
		deb = false
	end
end

if deb == true then
	print("Player is in safe zone")
else
	print("Player is not in safe zone")
end
end

2 comments

0

Ah yikes only downside is that this is on the player end- while I'm trying to get it on the server end.

0

@RjsMc, where it checks at the bottom, use a remoteEvent, or remote function to change the values on the server.

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