So, im making a script for a monster that attacks every player, and the part im trying to do is that if any player is in a certain range it will atack / use abilities on him, but for some reason this script isnt working and idk what to do
The Script
while true do
for _,player in ipairs(game.Players:GetPlayers()) do
local character = player.Character
local bix = script.Parent
local ms = false
local bs = false
local ss = false
if (character.Position-bix.Position).magnitude == 85 then
local random = math.random(0,1)
if random == 0 then
wait(0.1)
elseif random == 1 then
-- will do stuff
end
end
if (character.Position-bix.Position).magnitude == 3 then
if ms == false then
ms = true
-- part that isnt working
character.Humanoid:TakeDamage(35)
wait(2)
ms = false
end
end
end
end
Btw ms bs and ss are the attack cooldowns, so if its disabled it doesnt attacks
At your if statements, you should NOT use â==â. You should use â>=â or â<=â.
Itâs almost impossible that the magnitude is perfectly 3 or 85, you must check if itâs bigger or smaller than a number.
(Also instead of while true do use while wait(0.1) do or something like that so it doesnât lag your game that much)
Oh hey, iâve seen you around, btw your plugins are amazing lol
But then it will shoot multiple times non-stop right? or can i do like if .magnitude <= 85 and >= 50 then it will only shoot in that area and wont interrupt other attacks right?
while true do
wait()
for _,player in ipairs(game.Players:GetPlayers()) do
local character = player.Character or player.CharacterAdded:Wait()
local bix = script.Parent
local ms = false
local bs = false
local ss = false
if (character.PrimaryPart.Position-bix.Position).Magnitude == 85 then
local random = math.random(0,1)
if random == 0 then
wait(0.1)
elseif random == 1 then
-- will do stuff
end
elseif
(character.PrimaryPart.Position-bix.Position).Magnitude == 3 then
if ms then
ms = true
character.Humanoid:TakeDamage(35)
wait(2)
ms = false
end
end
end
end
Have you tried using print() to make sure the function is actually working when you think it is?
while true do
for _,player in ipairs(game.Players:GetPlayers()) do
local character = player.Character
local bix = script.Parent
local ms = false
local bs = false
local ss = false
if (character.Position-bix.Position).magnitude == 85 then
local random = math.random(0,1)
if random == 0 then
print("Random == 0")
wait(0.1)
elseif random == 1 then
print("Random == 1")
-- will do stuff
end
end
if (character.Position-bix.Position).magnitude == 3 then
if ms == false then
print("ms == false")
ms = true
-- part that isnt working
character.Humanoid:TakeDamage(35)
wait(2)
ms = false
else
print("ms == true")
end
end
end
end
Maybe the TakeDamage function isnât working for some reason? (It doesnât work sometimes when I use it.) You could try doing
while true do
for _,player in ipairs(game.Players:GetPlayers()) do
local character = player.Character
local bix = script.Parent
local ms = false
local bs = false
local ss = false
if (character.Position-bix.Position).magnitude == 85 then
local random = math.random(0,1)
if random == 0 then
print("Random == 0")
wait(0.1)
elseif random == 1 then
print("Random == 1")
-- will do stuff
end
end
if (character.Position-bix.Position).magnitude == 3 then
if ms == false then
print("ms == false")
ms = true
-- part that isnt working
character.Humanoid.Health -= 35
wait(2)
ms = false
else
print("ms == true")
end
end
end
end
Immediately what I noticed when testing out your script in Studio was the fact that the âwhile true doâ loop was exhausting the allowed execution time, causing the script to timeout. The second thing I noticed was youâre using âcharacter.Positionâ which would not be valid. The character model itself doesnât have a fixed âpositionâ, however you could fix this by using character.PrimaryPart.Position which uses the characterâs HumanoidRootPart to somewhat accurately determine its position in Workspace. Going back to the loop statement, I think you should handle this using a Player.PlayerAdded function instead of looping through all players infinitely.
As @Xsticcy correctly stated above, you shouldnât really be using == on your magnitude if statements. This will only execute if the magnitude is exactly that number which in most cases wonât be true.
Iâve fixed up the code which Iâll provide below using all info from above.
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character:WaitForChild('HumanoidRootPart',math.huge)
local bix = script.Parent
local ms = false
local bs = false
local ss = false
while ms == false and wait(0.15) do
if (character.PrimaryPart.Position-bix.Position).magnitude >= 85 then
local random = math.random(0,1)
if random == 0 then
wait(0.1)
elseif random == 1 then
-- will do stuff
end
end
if (character.PrimaryPart.Position-bix.Position).magnitude <= 5 then
if ms == false then
ms = true
-- part that wasn't before working
character.Humanoid:TakeDamage(35)
wait(2)
ms = false
end
end
end
end)
end)
I have tested this in Roblox Studio myself and can confirm that this solution works on my end.
ohh thx this worked! but could i make that part like if .magnitude <= 85 and >= 70 so it will only be in a flat torus-like area? making it not interrupt other attacks?
while task.wait() do
for _,player in ipairs(game.Players:GetPlayers()) do
local character = player.Character
local bix = script.Parent
local ms = false
local bs = false
local ss = false
if (character.Position-bix.Position).magnitude == 85 then
local random = math.random(0,1)
if random == 0 then
task.wait(0.1)
elseif random == 1 then
-- will do stuff
end
end
if (character.Position-bix.Position).magnitude == 3 then
if ms == false then
ms = true
-- part that isnt working
character.Humanoid:TakeDamage(35)
task.wait(2)
ms = false
end
end
end
end
Iâve added a yielding function the while loop, it should work as expected now.
if (character.Position-bix.Position).Magnitude <= 85 and (character.Position-bix.Position).Magnitude >= 70 then
Whenever you want to compare two or more conditions within the same conditional expression you need to reiterate what the conditions are being checked against in this case that would be â(character.Position-bix.Position).Magnitudeâ. Also remember to capitalise property names.