Not detecting if a player is near correctly

So recently, I’ve been working on a small project and tried to rewrite an old script. Originally it was gonna give a effect to the player if their close enough to it. But after rewriting the code, it no longer works and I’m confused on what I’m doing wrong. There are not errors in Workspace but no prints either. Any help is appreciated!

The script is a server script.
Script:

local RS = game:GetService("ReplicatedStorage")
local mod = require(RS.Modules.EffectMod)

local part = script.Parent
local Range = 10

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

game.Players.PlayerAdded:Connect(function(player)
	
	player.CharacterAdded:Connect(function(Char)
		
		task.spawn(function()
			
			while true do
				
				wait(0.25)
				
				if CheckForPlayer(player) then
					local Distance = player.DistanceFromCharacter(player, part.Position)

					if Distance <= Range then
						print("Distance:"..Distance..", Range:"..Range)

						--Irrelevant code
					end
				end
			end
		end)
	end)
end)

Hey there! I did some searching on the .DistanceFromCharacter and I think you may be using it incorrectly. From what I learned, the function only takes one parameter, which in your case is the part.Position. Try removing the player from the function and make that line local Distance = player.DistanceFromCharacter(part.Position)

1 Like

I couldn’t find any actual pages on the function, but I read through a few dev forums. I think .DistanceFromCharacter is depreciated. It may be better to use .magnitude to find the distance between player.Character.HumanoidRootPart.Position and part.Position.

This can be done as such.
(player.Character.HumanoidRootPart.Position - part.Position).magnitude

1 Like

tried to replace the DistanceFromCharacter() function with (Char.HumanoidRootPart.Position - part.Position).Magnitude, but for some reason i didn’t get any errors nor did it work. I don’t really know what else the problem could be

I recommend you print beforehand, also some things I recommend changing
(not required, but suggested)

image
Instead of this, you can just do
while task.wait(.25) do


Also, I recommend using :WaitForChild() here to check for the mod (might be the problem)

I added these prints to the script and they never showed up in the output. I believe it’s something dealing with the part being in ReplicatedStorage and cloned then sent to the Workspace. So I tried to wait for the parent to change and check if it’s in the workspace in a folder. Then start running the function to check if the player is near but it didn’t work out.

New code:

game.Players.PlayerAdded:Connect(function(player)
	print("Player added!")
	player.CharacterAdded:Connect(function(Char)
		print("Character added!")
		
		part.Changed:Connect(function()
			if part.Parent.Name == "Folder" then

				task.spawn(function()

					while task.wait(0.25) do

						if CheckForPlayer(player) then
							local Distance = (Char.HumanoidRootPart.Position - part.Position).Magnitude

							if Distance <= Range then
								print("Distance:"..Distance..", Range:"..Range)

								mod.GiveEffect(Char, "Poison", 1)
							end
						end
					end
				end)
			end
		end)
	end)
end)

Here, try this:

game.Players.PlayerAdded:Connect(function(player)
	print("Player added!")
	player.CharacterAppearanceLoaded:Connect(function(Char)
		print("Character added!")

		part.Changed:Connect(function()
			if part.Parent.Name == "Folder" then

				task.spawn(function()

					while task.wait(0.25) do

						if CheckForPlayer(player) then
							local Distance = (Char.HumanoidRootPart.Position - part.Position).Magnitude

							if Distance <= Range then
								print("Distance:"..Distance..", Range:"..Range)

								mod.GiveEffect(Char, "Poison", 1)
							end
						end
					end
				end)
			end
		end)
	end)
end)

All I did was change CharacterAdded to CharacterAppearanceLoaded.
If this doesn’t work, it might be this but yeah.
image

the thing is, it doesnt print the "Player added!" and "Character Added!" for some odd reason. I’m unsure if it’s due to the original being in replicatedstorage or not.

for context
The parts are in ReplicatedStoragemostly as templates and I clone the parts via script and put them at a random spot of the map. I’m unsure if that could be the reason the player is properly being defined or not but my answer is slowly leaning towards the part being in ReplicatedStorage.

Here’s the image (I scribbled out all the unneccessary stuff)
image

Where’s your script located? And what type is it (I know it won’t really matter but still)

in the part itself.
image

But I found a solution!
I just made the script a local script and defined the player from there. I then connected a RemoteEvent to the local script just to run the same effect function. Here’s the code now for anyone who wants to know

Script:

local RS = game:GetService("ReplicatedStorage")
local event = RS.Events.CreateEffect

local player = game.Players.LocalPlayer
local part = script.Parent
local effect = part.ParticleEmitter
local Range = 10

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

part.Changed:Connect(function()
	if part.Parent.Name == "Folder" then

		task.spawn(function()

			while task.wait(0.25) do

				if CheckForPlayer(player) then
					local Distance = (player.Character:WaitForChild("HumanoidRootPart").Position - part.Position).Magnitude

					if Distance <= Range then
						print("Distance:"..Distance..", Range:"..Range)
						
						event:FireServer(player, "Poison", 1)
					end
				end
			end
		end)
	end
end)

Why do you need a local script for this?

to define the local player. It made the script work so I’m not really questioning it

You can use a server-script in startercharacter

local Character = script.Parent
local Player = game:GetService("Players"):GetPlayerFromCharacter(Character)
`
1 Like

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