Script not working but no errors in output?

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

1 Like

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)

2 Likes

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?

1 Like

Isn’t it Magnitude and not magnitude?

Here’s how I would write it:

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
3 Likes

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

yeah i added and what isnt working is the hit thing, idk why

well but like i said to @uraveragecatboy i tried adding print statements and what isnt working is the hit thing

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

Not sure if that works but it’s worth trying

1 Like

This is not true. Using == is perfectly normal and refers to ‘equals exactly’.

Edit: I didn’t read your entire comment, my bad. You were correct in saying that they shouldn’t be using == in their magnitude if statements.

3 Likes

No it won’t. It will only shoot if the magnitude is equal or smaller than 85 AND equal or bigger than 50.

1 Like

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.

5 Likes

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?

1 Like
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.

1 Like

You’ll need to perform those checks like this.

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.

1 Like

Have you even tried my code. It looks like you didn’t.

2 Likes

Yep! You can do this by adding these lines:

if (character.PrimaryPart.Position-bix.Position).magnitude >= 70 and (character.PrimaryPart.Position-bix.Position).magnitude <= 85 then
1 Like

Well i tried but it didnt work, but yeah u dont gotta worry anymore as @Zivao made a fix!

1 Like

Oh thx this worked! i though i wouldnt need the (arguments) for the other part

2 Likes