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

When picking up a spinning tool, it doesn't stop spinning despite I have a break in the loop?

Asked by 3 years ago

Hello, thanks for clicking on my thread. I have an issue with this code that I thought would work but doesn't. What happens is that when grabbing the tool, I thought adding break would stop the spinning but nothing worked. Instead of breaking the loop like it should. It continues to rotate and starts spinning the player when equipped. Please help!

The script is located inside of a handle and the handle is in a tool.

Code:

local handle = script.Parent


handle.CanCollide = false


handle.Touched:Connect(function()
    handle.Anchored = false
end)


while true do -- problem
    if handle.Touched == true then
        break
    else
        handle.CFrame = handle.CFrame * CFrame.fromEulerAnglesXYZ(0.1, 0.1, 0.1)
        wait()
    end
end
0
When you say 'grabbing', do you mean equipping the tool? If so, you should use events like tool.Equipped. Wait how does handle spin when not equipped. Oh and maybe put break into the event handle.Touched, because the event and loop are waiting for same thing. Phyrixia 51 — 3y

3 answers

Log in to vote
0
Answered by 3 years ago

This is an easy solution. The problem is that ".Touched" isn't a value, it's an event rather so to speak so it gets called on but isn't a true or false value. Instead you can set a variable to true calling something like "notTouched" and set at into the while loop just like the following:

handle = script.Parent
notTouched = true --While loops only run as long as a value is true

handle.CanCollide = false

handle.Touched:Connect(function()
    handle.Anchored = false
    notTouched = false --Sets the value to false therefore making the while loop break
end)

while notTouched do --Keeps running as long as "notTouched" is true
    whateverCFrameValueYouHave = WhateverCFrameValueYouChange
    wait()
end

If you have any questions or issues contact me. ;)

0
It worked! Thank you so much :) WyattDevv 29 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Instead of doing that you can do

while handle.Touched == false do -- you won't need a if statement

If way 1 doesn't works here is another way

while true do
handle.Touched:Connect(function()
break
end)
end
0
I'm not sure how Roblox would handle this so I wouldn't reccomend this. The problem is that everytime the loop runs you are basically redefining the ":Connect" function whch can result in serious performance loss or even a crash. lazycoolboy500 597 — 3y
Log in to vote
0
Answered by
0msh 333 Moderation Voter
3 years ago

or you can just destroy the script because it looks like you have no other functions in there

local handle = script.Parent


handle.CanCollide = false


while wait() do
    handle.CFrame = handle.CFrame * CFrame.fromEulerAnglesXYZ(0.1, 0.1, 0.1)
        script.Parent.Touched:Connect(function()
            handle.Anchored = false
                script:Destroy()
        end)
end
0
Not bad but as I said earlier -- I would not reccomend putting the ":Connect()" function within a loop as this would redefine it each time it's looped which can cause serious performance loss or even result in a crash (depending on how Roblox handles it). lazycoolboy500 597 — 3y

Answer this question