Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I update .Magnitude more effectively?

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.

4 comments

0

the script looks completely fine to me. are you resetting B1 to false after it dies?

2

also, your remote events are completely backwards. you should be doing 100% of this on the roblox server, this could get hacked in an instant. they can just change the magnitude to always be out of range... or not fire remotes at all.

0

Ok, so how would I get the player in the sever script? Would I have to use OnPlayerAdded?

1

I changed it to .PlayerAdded and it all runs from the server script now

1 answer

✓ Accepted answer

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?

5 comments

0

Yeah there are no errors, it just doesnt seem to run when it respawns near the player. I tried reseting B1 like Speedmask said, but it didn't seem to fix it. I will try using FindNearestPlayer. Thanks!

0

I will try seeing what those variables return

0

Let me know if you have trouble (I may need to see your latest scripts if it's still not working)

0

Ok thanks, ill let you know

0

I printed out variables and found out where it was choking at, I fixed it now. Thank you

This discussion is preserved as it was. New answers, comments, and votes are closed.