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 4 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:

01local handle = script.Parent
02 
03 
04handle.CanCollide = false
05 
06 
07handle.Touched:Connect(function()
08    handle.Anchored = false
09end)
10 
11 
12while 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
19end
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 — 4y

3 answers

Log in to vote
0
Answered by 4 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:

01handle = script.Parent
02notTouched = true --While loops only run as long as a value is true
03 
04handle.CanCollide = false
05 
06handle.Touched:Connect(function()
07    handle.Anchored = false
08    notTouched = false --Sets the value to false therefore making the while loop break
09end)
10 
11while notTouched do --Keeps running as long as "notTouched" is true
12    whateverCFrameValueYouHave = WhateverCFrameValueYouChange
13    wait()
14end

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

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

Instead of doing that you can do

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

If way 1 doesn't works here is another way

1while true do
2handle.Touched:Connect(function()
3break
4end)
5end
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 — 4y
Log in to vote
0
Answered by
0msh 333 Moderation Voter
4 years ago

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

01local handle = script.Parent
02 
03 
04handle.CanCollide = false
05 
06 
07while 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)
13end
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 — 4y

Answer this question