The following code checks ever 0.5 seconds to see if the SelectedSlot
has a Y position of 0, 0. If it does, then all tools currently equipped by the player will be unequipped.
However this crashes my game. I have a feeling that this while
loop is the reason.
while true do if SelectedSlot ~= nil then ScaleX = SelectedSlot.Position.X.Scale OffsetX = SelectedSlot.Position.X.Offset ScaleY = SelectedSlot.Position.Y.Scale OffsetY = SelectedSlot.Position.Y.Offset if OffsetY == 0 then Player.Character.Humanoid:UnequipTools() end wait(0.5) end end
Any solutions to make this loop work or alternatives that will serve the same purpose? Thanks in advance.
There are two solutions instead of using while true do
. You could use RenderStepped
, which is basically wait(0.000001), or you can use while wait() do
.
while wait() do --do something end
game:GetService("RunService").RenderStepped:Connect(function() --do something end)
I think adding a wait()
makes a big difference as it doesn't crash your game as easily. Please accept my answer if this helped! If it didn't, please comment! Thanks!