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:
01 | local handle = script.Parent |
02 |
03 |
04 | handle.CanCollide = false |
05 |
06 |
07 | handle.Touched:Connect( function () |
08 | handle.Anchored = false |
09 | end ) |
10 |
11 |
12 | while true do -- problem |
13 | if handle.Touched = = true then |
14 | break |
15 | else |
16 | handle.CFrame = handle.CFrame * CFrame.fromEulerAnglesXYZ( 0.1 , 0.1 , 0.1 ) |
17 | wait() |
18 | end |
19 | end |
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:
01 | handle = script.Parent |
02 | notTouched = true --While loops only run as long as a value is true |
03 |
04 | handle.CanCollide = false |
05 |
06 | handle.Touched:Connect( function () |
07 | handle.Anchored = false |
08 | notTouched = false --Sets the value to false therefore making the while loop break |
09 | end ) |
10 |
11 | while notTouched do --Keeps running as long as "notTouched" is true |
12 | whateverCFrameValueYouHave = WhateverCFrameValueYouChange |
13 | wait() |
14 | end |
If you have any questions or issues contact me. ;)
Instead of doing that you can do
1 | while handle.Touched = = false do -- you won't need a if statement |
If way 1 doesn't works here is another way
1 | while true do |
2 | handle.Touched:Connect( function () |
3 | break |
4 | end ) |
5 | end |
or you can just destroy the script because it looks like you have no other functions in there
01 | local handle = script.Parent |
02 |
03 |
04 | handle.CanCollide = false |
05 |
06 |
07 | while wait() do |
08 | handle.CFrame = handle.CFrame * CFrame.fromEulerAnglesXYZ( 0.1 , 0.1 , 0.1 ) |
09 | script.Parent.Touched:Connect( function () |
10 | handle.Anchored = false |
11 | script:Destroy() |
12 | end ) |
13 | end |