Why is this not working and how to fix?

Hello, I want to make a safe zone type of thing where if the player is in it, they get infinite steps, however, it is not detecting whenever the player is in it. Can someone help? Here is my current code:

local SoundRegionsWorkspace = workspace.SafeZones
local plr = game.Players.LocalPlayer
local steps = plr.PlayerGui.StepsGUI.Frame.TextLabel
local stagetrans = plr.PlayerGui.StageTransfer.CurrentStage
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Whitelist
overlapParams.FilterDescendantsInstances = { plr.Character }
local mod = script.Parent:WaitForChild('StoreSteps')
local data = require(mod)
local foundCharacter = false


for _, child in ipairs(SoundRegionsWorkspace:GetChildren()) do
	if not child:IsA('BasePart') then continue end
	local foundParts = workspace:GetPartsInPart(child, overlapParams)
	if foundCharacter then
		steps.Text = if foundCharacter then 'inf' else tostring(data[stagetrans.Text])
		break
	end
end

This line right here:
if not child:IsA('BasePart') then continue end

would make the entire function invalid since what you are trying to do is find every part in the child part BUT only if the child is not a part.

Try changing it to
if child:IsA('BasePart') then continue end

Still does not work. wewfefewwfe

I believe it’s because it only checks if the player is inside the SoundRegion once the script starts up, no?

also change
if foundCharacter then steps.Text = if foundCharacter then 'inf' else tostring(data[stagetrans.Text]) break end

to
local Text = tostring(data[stagetrans.Text]) if foundCharacter then Text = "inf" end steps.Text = Text

Still does not work.wfewfefwewefwef

Try putting the for _ child... loop in a while true do loop, just to see if that fixes it. Should work so it’ll need cleaning up afterwards

did that, still does not detect when player is in part.

Strange. You get no errors of any kind either?

Try this

while wait() do
	SoundRegionsWorkspace.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			steps.Text = "inf"
		end
	end)
	SoundRegionsWorkspace.TouchEnded:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			steps.Text = "whatever you want them to be"
	end)
end

Nope. No errors in output. Ill send a picture
image

I have multiple sound regions in the map so this would only work for one.

edit: also this is a local script

Don’t use a local script. Use a normal script and put it inside each of the zones. It should work then.

I want to make it efficient and only in one script so I don’t really want to do that. Should I use a for loop gathering it all?

Touched is very inconsistent so this is a bad idea.

Oh ok.

If you want to do that I suggest make a starter player script and fire the event whenever say the torso is in the area (this depends on whether your game is r6 or r15)

Like this:

while wait() do
	local Player = script.Parent.Parent
	Player.Character:FindFirstChild("Torso").Touched:Connect(function(hit)
		if hit.Name == "Name of the sound region" or "Other names of sound regioins" then
			steps.Text = "inf"
		end
	end)
end

You would have to add more “or”'s for every region you want the player to have inf steps in

Also idk whether this should be a local or a normal script but you can test both. I dont think it makes a difference really

If it doesn’t work it’s probably because i got the wrong number of parents. Carry on adding more till you get it.

I tried optimizing your script since I couldnt access playergui with script.parent.parent no matter how many parents there were and its not working. could u tell me whats wrong?

local SafeZones = workspace.SafeZones
local plr = game.Players.LocalPlayer
local steps = plr.PlayerGui:WaitForChild("StepsGUI").Frame.TextLabel
local stagetrans = plr.PlayerGui:WaitForChild("StageTransfer").CurrentStage
local mod = script.Parent:WaitForChild('StoreSteps')
local data = require(mod)

print('loop')
while wait() do
	plr.Character:FindFirstChild("Torso").Touched:Connect(function(hit)
		print('touch')
		if hit.Name == "sf" then
			steps.Text = "inf"
		else
			steps.Text = tostring(data[stagetrans.Text])
		end
	end)
	print('end')
	--if steps.Text == tostring(data[stagetrans.Text]) then
	--	break
end

Its not printing anything, not even “loop”

It probably depends on where the script it. You can only use LocalPlayer if the script/localscript is in startergui, starterplayerscripts or startercharacterscripts.

Another thing. You might want Rename the mod variable. Use a capital letter. It’s showing up in blue on the devforum editor so I’m assuming it means something else.

1 Like

If you let it run I’m assuming it will give you an error. What I think is happening is its waiting for one of your WaitForChild’s to find what its looking for but it doesnt exist.

There should be an error.