Script only works after timer ends?

i made a script that allows the player to pick up a feather item and it makes them weightless. after the player picks it up a 10 second timer starts and they should be able to float only while holding space.

however the script i made somehow only lets the player float after the 10 second timer ends. I can’t seem to find out why thats happening, since the script is setup to where it only works while the task.wait(10) is running.

this is the script:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local plrchar = script.Parent
local lclplayer = Players:GetPlayerFromCharacter(plrchar)

local RemoteEvent = script.RemoteEvent
local dbljump = script.Parent.MultipleJump

local active = false

RemoteEvent.OnClientEvent:Connect(function(player)
	print("featherweight active")
	active = true
        local humanoid = plrchar:FindFirstChildOfClass("Humanoid")
        if humanoid then
			while task.wait(10) and active do
				local jumpheight = humanoid.JumpHeight
				local originalGravity = workspace.Gravity
				local gravityChanged = false
				
				UserInputService.InputBegan:Connect(function(input, gameProcessed)
					if input.KeyCode == Enum.KeyCode.Space and not gameProcessed then
						if humanoid:GetState() == Enum.HumanoidStateType.Freefall then
							--dbljump.Enabled = false
							workspace.Gravity = 20
							gravityChanged = true

					end
					end
					
				UserInputService.InputEnded:Connect(function(input, gameProcessed)
					if input.KeyCode == Enum.KeyCode.Space and gravityChanged then
						--dbljump.Enabled = true
						workspace.Gravity = originalGravity
						gravityChanged = false

					end
				end)
				end)
				workspace.Gravity = originalGravity
				print("over")
				active = false
            end
        end
end)


The script works thankfully but its only after the “over” is printed.

1 Like
while task.wait(x) do
end

is about the same as

while true do
    task.wait(x)
end

you could use task.delay() to disconnect the connections after 10 secs & change values?

1 Like

task.wait(10) will wait 10 seconds before moving on.

Try using a counter instead.

Place it before the last end in your script.

for i = 10, 0, -1 do end -- counter
active = false

Remove the task.wait(10)

1 Like

could you elaborate on this please? i dont know how to use task.delay but when I tried, it just did the same thing task.wait did

1 Like

I tried this and it executes the script but it doesn’t end.

1 Like

replace this with

while active do

and put

task.wait(10)
active = false

at the end of the loop.

This way, the player is able to float until the cooldown of 10 seconds goes by, resulting in ending the loop by setting active to false.

2 Likes

sorry for the late reply. this works for me except for the end, the gravity still changes when holding space even when the script prints over and I have no idea why. could you help?

edit.
i ended up fixing this by creating a new parent localscript that handles the remote event and disables the previous script after 10 seconds. i’m still having the problem where the script changes the gravity for every other object in the game though (im not sure if it effects other players but it’s safe to assume that it does)

another problem im having is the gravity changes for everything else in the world when activated, even though the script is in a localscript

video: