The titles a little vague, my apologies I didn't know how to word it. Basically, I'm creating a boss, and while the player is within 50 studs of the boss it will move towards the player constantly, this works fine as well as the attacks (I don't want to use pathfinding, so I'm using :MoveTo()). The issue is that when the boss respawns if the player is already within 50 studs the boss will sit there dormant. Here is the code I'm using to do this:
local Found = false local B1 = false local attacking = false while wait(1) do local DBMpos = workspace:FindFirstChild("CaptainTennileImposter").HumanoidRootPart.Position local PlayerPos = game.Players.LocalPlayer.Character.HumanoidRootPart.Position local magnitude = (DBMpos - PlayerPos).Magnitude --Make sure he's alive if workspace:FindFirstChild("CaptainTennileImposter").Humanoid.Health > 0 then --If the players close then he'll move if magnitude <= 50 then if B1 then if (DBMpos - PlayerPos).Magnitude <= 5 then game.ReplicatedStorage.BossReps.DBMAttack:FireServer() end elseif not B1 then B1 = true game.ReplicatedStorage.BossReps.InDarkBlueMoon:FireServer(game.Players.LocalPlayer) end else B1 = false end end end
I originally had it at wait() but it ran better to wait(1). What can I do to make it so that If the boss respawns and the player is within the 50 studs it will move? I have a print statement setup in the server side to detect when the player first triggers movement, but this doesn't fire when the player starts within 50 studs of the boss.
I see in the comment you've now got server code. The ideal server code will iterate over all players, see if they have a character (with a HumanoidRootPart), and calculate the distance to each part (selecting the player with the least). Zombie scripts almost always have a function that does this (findNearestPlayer
or similar) if you want to see some code (just beware of malicious scripts as usual). You can have it keep using just your character (as you do in your script) for testing, but this won't work for any other players.
Your script does look fine otherwise, so you may want to print out the values of different variables. Have you checked the Output window for errors?