Hi can someone help me with this script

Hi so ive got this script where it checks if your in the range in the magnitude but how can i make it so it keeps on checking until someone gets to the magnitude

Heres the code:

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse() 
local Equiped = false
local character = Player.Character 

if not character or not character.Parent then
	character = Player.CharacterAdded:wait() 
end
local Enabled = false

local Power = 10




local Holding = false
Mouse.Button1Down:Connect(function() 
	Power = 10
	
	
	Holding = true
	while Holding == true do
	
		wait()
		print(Power)

				Power += 3.42
		
				
			
		if Power >= 80 then
			Power = 80
			
			end
		end
	

end)

Mouse.Button1Up:Connect(function()
		Holding = false


		if (character["Right Leg"].Position - game.Workspace.Ball.Position).Magnitude <= 2 then
		game:GetService("ReplicatedStorage"):FindFirstChild("RemoteEvent"):FireServer(game.Workspace.Ball)

			local F = Instance.new("BodyVelocity") -- Creates a Force
			F.P = 9
			F.Parent = game.Workspace.Ball -- Makes the force act on the ball
			F.Name = script.Name -- Make the force's name the script's name
			F.velocity = character.HumanoidRootPart.CFrame.lookVector * Power + Vector3.new(0,Power - 40,0)  -- The force is where the Right Leg is looking (so if your right leg was looking towards the goal then the ball would go to the goal) and its given a certain value to go against (the higher the number, the faster the ball would go)


			F.maxForce = Vector3.new(math.huge,math.huge,math.huge) -- The maximum force it's allowed to give the ball 
			game.Debris:AddItem(F,0.3) -- This destroys the Force within 0.3 seconds without stopping the code so it doesn't keep the ball moving
			
		end
		
	end


end)

    
		

5 Likes

You cold use a while loop with magnitude reached as condition

4 Likes

Could you show me how it would look the code please.

1 Like
while task.wait(1) do
    print("Hello world!")
end
1 Like

Maybe you could try this:

HumanoidRootPart:GetPropertySignalChanged(“Position”):Connect(function()
-- check distance
if (HumanoidRootPart.Position - workspace.Ball.Position).Magnitude < integer then
-- code
end
end)

P.S. this fires every time the player moves

Dunno if I’d recommend this exact method for performance

local condition = false

while not condition do
wait()
if magnitude < x then
condition = true
end
end)

How could i make it so it destroys the loop after some time so it just doesnt carry on for the game.

break

If you’re using @fontorax‘s method you could use the keyword “break”

break escapes the loop (similar to RbxEventSignal:Disconnect())

Ive never used this before where would i put it in the code?

You’d put it wherever you want the code to stop. Here’s an example:

while blah do
if myCondition == true then
break
end
end

print(“Hello World!”) -- Runs after myCondition == true

Also, this isn’t the most performance safe method, the while loop might crash studio for you.

If i make it break like 0.4 seconds later will it keep checking the magnitude until it gets to 0.4 seconds?

Uhh, you’d have to create a thread inside the while loop that waits 0.4 seconds; if you do, yeah it’ll keep checking for 0.4 seconds then stop.

Maybe if that doesn’t work maybe you could try this :wink:

HumanoidRootPart:GetPropertySignalChanged(“Position”):Connect(function()
-- check distance
task.spawn(function()
task.wait(0.4)
return
end)
if (HumanoidRootPart.Position - workspace.Ball.Position).Magnitude < integer then
-- code
end
end)

Could try the code or not, that’s on you; just my recommendation :slight_smile:

What im trying to achieve here is so it loops for 0.4 seconds and keeps on checking the magnutude then the loop just breaks after

Repeat
print(‘hi’)
until magnitude found

breaks after what is the question.

You need to setup some condition that needs to be met before it breaks.

I used that code now the ball just doesnt kick.

Oh, just remove the “task.spawn()” part cause it breaks the loop like instantly. Try again ig

P.S. I added it because you were talking about that

Remove this:

task.spawn(function()
task.wait(0.4)
return
end)

I’ll be awaiting your response :relieved:

Wait so remove the whole selection youve just typed

Wait don’t do that, I meant delete this section (not the entire thing):


task.spawn(function()
task.wait(0.4)
return
end)

—I’ll be awaiting your response :slight_smile:—

It says “GetPropertySignalChanged is not a valid member of part Workspace.aidanoldo.HumanoidRootPart”