Script not running [solved]

  1. What do you want to achieve? A gui which bobs in a sine wave, changing it’s rotation to fit the sin value

  2. What is the issue? the code seems to work fine but when i add the sine wave script i wrote it doesn’t run, no error in console or anything

  3. What solutions have you tried so far? i’ve tried asking for help in various places but no one seems to want to help, so i went here!

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local UserInputService = game:GetService("UserInputService")


local button = script.Parent
local bob = false

local i = 0


local function bobthecard()
	bob = true
	print('bobbing')
end
local function unbobthecard()
	bob = false
	print('not bobbing')	
end

--code which is making it not run
while bob == true do
	wait()
	i = i + 1
	script.Parent.Rotation = math.sin(i/8) * 6 		
end
while bob == false do
	wait()
	i = 0
	script.Parent.Rotation = 0
end
-- have no idea why, but when i remove this it works fine

button.MouseEnter:Connect(bobthecard)

button.MouseLeave:Connect(unbobthecard)

1 Like

This is probably because the while loop simply won’t run since it’s already tried to run but the value was false. I would recommend putting this in the MouseEnter connection or a simple fix could be this:

while true do 
   task.wait()
   if bob then
      -- bob is true
   else
      -- bob is false
   end
end

i dunno if it’s just me being bad at code but i’ve tried


local function bobthecard()
	bob = true
	print('bobbing')
	script.Parent:TweenPosition(UDim2.new(0.5,0,0.5,0))
	
	while true do 
		task.wait()
		if bob then
			i = i + 1
			script.Parent.Rotation = math.sin(i/8) * 6
		else
			i = 0
			script.Parent.Rotation = 0
		end
	end
	
end
local function unbobthecard()
	bob = false
	print('not bobbing')	
	script.Parent:TweenPosition(UDim2.new(0,0,0,0))
end

and


local function bobthecard()
	bob = true
	print('bobbing')

	
	
end
	while true do 
		task.wait()
		if bob then
			i = i + 1
			script.Parent.Rotation = math.sin(i/8) * 6
		else
			i = 0
			script.Parent.Rotation = 0
		end
	end
local function unbobthecard()
	bob = false
	print('not bobbing')	

end

and both seem to not work, it prints the message however

Try this maybe? It should work

--// Services
local UserInputService = game:GetService("UserInputService")

--// Vars
local button = script.Parent
local bob = false

local RotationAmount = 5 -- Edit this if needed

--// Functions
local function bobthecard()
	bob = true
	print('bobbing')
end

local function unbobthecard()
	bob = false
	print('not bobbing')	
end

--// Mouse Connections
button.MouseEnter:Connect(bobthecard)
button.MouseLeave:Connect(unbobthecard)

--// While Loop
while true do
	task.wait()
	if bob then
      for i = 1, RotationAmount do
         if not bob then continue end -- Just in case bob is false during the for loop
         task.wait()
         script.Parent.Rotation = math.sin(i/8) * 6 	
      end
    else
       script.Parent.Rotation = 0
    end	
end
2 Likes