Part finding player's radius not working

So my goal was to make a part that will distort the player’s vision and slowly drain their health whenever their within range of the block. The part works when the player first joins however after the player dies, it doesn’t work at all and doesn’t show any bugs on both the client and server side. I’ve spent countless hours trying to fix this bug but haven’t found a good solution. Any help will be very much appreciated!

Picture:
image

Script:

local RS = game:GetService("RunService")
local TS = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local Char = player.Character or player.CharacterAdded:Wait()

local InRange = false
local Blur = game.Lighting.Blur
local ColorCorrection = game.Lighting.ColorCorrection
local Range = 10

local function CheckForPlayer(Player) --Checks if the player is fully loaded in and alive
	if Player then
		if Player.Character then
			if Player.Character:WaitForChild("Humanoid").Health > 0 then
				return true
			else 
				return false 
			end
		else
			return false
		end
	else
		return false
	end
end

Blur.Size = 0

Char:WaitForChild("Humanoid").Changed:Connect(function()
	if Char:WaitForChild("Humanoid").MoveDirection.Magnitude ~= 0  then --Checks if the player is moving
		if CheckForPlayer(player) then --Checks for the player
			local Distance = player.DistanceFromCharacter(player, script.Parent.Position) --Defines the distance

			if Distance <= Range then
				if InRange == false then
					InRange = true

					--tweens

					while true do --Damages the player every 1/4 second.
						if InRange then
							wait(0.25)
							print(player.Name.." is in range")
							Char:WaitForChild("Humanoid").Health -=1
						else
							break
						end
					end
				end
			else --If the player is no longer within distance.
				if InRange == true then 
					InRange = false
					--more tweens
				end
			end
		else
			return
		end
	end
end)

Char:WaitForChild("Humanoid").Died:Connect(function()
	InRange = false
	--tweens
end)

Maybe this will help: put the character’s events inside a function, and then call that function when the character is added. Here is an example:

local function onCharAdded(character)
	-- Code here.
end

onCharAdded(player.Character or player.CharacterAdded:Wait())

player.CharacterAdded:Connect(onCharAdded)

Or put the script inside StarterCharacterScripts so that the script will automatically renew when the player dies and respawns.

2 Likes

So I tried to redefine the player’s character model every time the player’s character was added to the workspace. But it only lead to the same thing, no errors but still doesn’t work after the player dies.

here’s what the code looks like now

--<Variables>
local RS = game:GetService("RunService")
local TS = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local Char = player.Character or player.CharacterAdded:Wait()
local InRange = false
local Blur = game.Lighting.Blur
local ColorCorrection = game.Lighting.ColorCorrection
local Range = 10

--<Functions>--
local function CheckForPlayer(Player)
	if Player then
		if Player.Character then
			if Player.Character:WaitForChild("Humanoid").Health > 0 then
				return true
			else 
				return false 
			end
		else
			return false
		end
	else
		return false
	end
end

local function OnCharAdded(character)
	if player then
		if player.Character then
			Char = character
		end
	end
	
	print(character)
	return character
end

--<Script>--
Blur.Size = 0

OnCharAdded(player.Character)

Char:WaitForChild("Humanoid").Changed:Connect(function()
	if Char:WaitForChild("Humanoid").MoveDirection.Magnitude ~= 0  then
		if CheckForPlayer(player) then
			local Distance = player.DistanceFromCharacter(player, script.Parent.Position)

			if Distance <= Range then
				if InRange == false then
					InRange = true

					--tweens

					while true do
						if InRange then
							wait(0.25)
							print(player.Name.." is in range")
							Char:WaitForChild("Humanoid").Health -=1
						else
							break
						end
					end
				end
			else

				if InRange == true then
					InRange = false
					--tweens
				end
			end
		else
			return
		end
	end
end)

Char:WaitForChild("Humanoid").Died:Connect(function() --Resets the player's 
	InRange = false
	--tweens
end)

player.CharacterAdded:Connect(function(character) --Updates the player's character upon loading
	OnCharAdded(character)
end)

Try this:

local function onCharAdded(Char)
	Char:WaitForChild("Humanoid").Changed:Connect(function()
			if Char:WaitForChild("Humanoid").MoveDirection.Magnitude ~= 0  then
				if CheckForPlayer(player) then
					local Distance = player.DistanceFromCharacter(player, script.Parent.Position)
		
					if Distance <= Range then
						if InRange == false then
							InRange = true
		
							--tweens
		
							while true do
								if InRange then
									wait(0.25)
									print(player.Name.." is in range")
									Char:WaitForChild("Humanoid").Health -=1
								else
									break
								end
							end
						end
					else
		
						if InRange == true then
							InRange = false
							--tweens
						end
					end
				else
					return
				end
			end
		end)
		
		Char:WaitForChild("Humanoid").Died:Connect(function() --Resets the player's 
			InRange = false
			--tweens
		end)
end

onCharAdded(player.Character or player.CharacterAdded:Wait())

player.CharacterAdded:Connect(onCharAdded)

Updating the Char variable will not update what is already written, so you need to rewrite (re-call) the events related to the character.

2 Likes

So now the code doesn’t activate at all. I believe it’s because its only called whenever the player’s character is added.

Where are you running this script in?

So i tried your second solution and it works, just it insta-kills the player instead of slowly draing their health.
image

New Script:

--<Variables>
local RS = game:GetService("RunService")
local TS = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local Char = player.Character or player.CharacterAdded:Wait()
local InRange = false
local Blur = game.Lighting.Blur
local ColorCorrection = game.Lighting.ColorCorrection
local Range = 10

--<Functions>--
local function CheckForPlayer(Player)
	if Player then
		if Player.Character then
			if Player.Character:WaitForChild("Humanoid").Health > 0 then
				return true
			else 
				return false 
			end
		else
			return false
		end
	else
		return false
	end
end

--<Script>--
Blur.Size = 0

Char:WaitForChild("Humanoid").Changed:Connect(function()
	if Char:WaitForChild("Humanoid").MoveDirection.Magnitude ~= 0  then
		if CheckForPlayer(player) then
			for _, part in pairs(game.Workspace.Folder:GetChildren()) do
				if part:IsA("Part") and part.Name == "Toxin" then
					
					local Distance = player.DistanceFromCharacter(player, part.Position)

					if Distance <= Range then
						if InRange == false then
							InRange = true

							--Tweens

							while true do
								if InRange then
									wait(0.25)
									print(player.Name.." is in range")
									Char:WaitForChild("Humanoid").Health -=1
								else
									break
								end
							end
						end
					else

						if InRange == true then
							InRange = false
							--Tweens
						end
					end
				end
			end
		else
			return
		end
	end
end)

Char:WaitForChild("Humanoid").Died:Connect(function() --Resets the player's 
	InRange = false
	--Tweens
end)

originally i was running the code in a local script within the part. The part was stored in ReplicatedStorage, and it would be moved to a folder in workspace.

So, I think the problem is the the player’s character going nil after the first time they spawn. You may need to update the Char variable with the new Player.Character within the CheckPlayer function.

Something like this:

local function CheckForPlayer(Player) --Checks if the player is fully loaded in and alive
	if Player then
		if Player.Character then
			if Player.Character:WaitForChild("Humanoid").Health > 0 then
				Char = Player.Character
				return true
			else 
				return false 
			end
		else
			return false
		end
	else
		return false
	end
end
1 Like

I got the code to work. And I put the script inside the part into the StarterPlayerScripts.

Here is the edited code:

local RS = game:GetService("RunService")
local TS = game:GetService("TweenService")
local RP = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer
local InRange = false
local Blur = game.Lighting.Blur
local ColorCorrection = game.Lighting.ColorCorrection
local Range = 10

local part = -- [The part's location]
part.Parent = workspace

local function CheckForPlayer(Player)
	if Player then
		if Player.Character then
			if Player.Character:WaitForChild("Humanoid").Health > 0 then
				return true
			else 
				return false 
			end
		else
			return false
		end
	else
		return false
	end
end

local function onCharAdded(Char)
	print("Character Added.")
	
	Char:WaitForChild("Humanoid").Changed:Connect(function()
		if Char:WaitForChild("Humanoid").MoveDirection.Magnitude ~= 0  then
			if CheckForPlayer(player) then
				local Distance = player.DistanceFromCharacter(player, part.Position)

				if Distance <= Range then
					if InRange == false then
						InRange = true

						--tweens

						while true do
							if InRange then
								wait(0.25)
								print(player.Name.." is in range")
								Char:WaitForChild("Humanoid").Health -=1
							else
								break
							end
						end
					end
				else

					if InRange == true then
						InRange = false
						--tweens
					end
				end
			else
				return
			end
		end
	end)

	Char:WaitForChild("Humanoid").Died:Connect(function() --Resets the player's 
		InRange = false
		--tweens
	end)
end

onCharAdded(player.Character or player.CharacterAdded:Wait())

player.CharacterAdded:Connect(onCharAdded)

1 Like

It worked! Thank ya so much for your help! This saved alot of time! I also wanna thank everyone else who helped me as well. I appreciate it more than you can ever think

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.