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)
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
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 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)
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.
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)
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)