Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I update .Magnitude more effectively?

Asked by 3 years ago

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.

0
the script looks completely fine to me. are you resetting B1 to false after it dies? Speedmask 661 — 3y
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. Speedmask 661 — 3y
0
Ok, so how would I get the player in the sever script? Would I have to use OnPlayerAdded? pokemine1o9 44 — 3y
1
I changed it to .PlayerAdded and it all runs from the server script now pokemine1o9 44 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago

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?

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! pokemine1o9 44 — 3y
0
I will try seeing what those variables return pokemine1o9 44 — 3y
0
Let me know if you have trouble (I may need to see your latest scripts if it's still not working) chess123mate 5873 — 3y
0
Ok thanks, ill let you know pokemine1o9 44 — 3y
0
I printed out variables and found out where it was choking at, I fixed it now. Thank you pokemine1o9 44 — 3y
Ad

Answer this question