Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My script is only changing gui size once when i press Q how can i fix this?

My script only changes the gui once when i press Q and i dont know how to fix it

stamina = 100
local play = game.Players.LocalPlayer
local user = game:GetService("UserInputService")
local key = Enum.KeyCode.Q
local Orange = script.Parent 
user.InputBegan:Connect(function(input,gameprocessed)
	if input.UserInputType == Enum.UserInputType.Keyboard
and	 input.KeyCode == Enum.KeyCode.Q then
	print(stamina)
		stamina = stamina - 10  
 Orange.Size = UDim2.new(1 - 0.111 ,1,1,0)
			if stamina  < 100  
				then
				wait(2) 
			stamina = stamina + 10 
			 Orange.Size = UDim2.new(1 + 0.001 ,1,1,0)
			if stamina == 100 then  	
    end end end end)

3 comments

0

I only want it to preform when Q is pressed

0

Do you want it so this function only runs once? Since it seems that a player can just spam Q and make stamina drop well below zero

0

I want it so that everytime Q is pressed the gui changes size

1 answer

✓ Accepted answer

GENERAL PRACTICE

You've already defined the key for the Enum, though you didn't use it in the UIS check

Make "stamina" a Local Variable

You have an unnecessary if-then statement

if stamina == 100 then
end

Your stamina can drop below 0, so you should have a check to make sure that this cannot occur, or else at some point, the gui will not be visible to the player for a long period of time if they spam the "Q" Key.

ISSUES

Your set amounts for the Size are the exact same for losing stamina and gaining it back. Instead, use a percentage to calculate the frame change.

REVISED LOCAL SCRIPT

local play = game:GetService("Players").LocalPlayer
local user = game:GetService("UserInputService")
local key = Enum.KeyCode.Q
local Orange = script.Parent 
local stamina = 100

user.InputBegan:Connect(function(input, event)
	if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == key then
		if stamina >= 10 then
			stamina = stamina - 10  
			Orange.Size = UDim2.new((stamina / 100), 0, 1, 0)
			if stamina < 100 then
				wait(2) 
				stamina = stamina + 10 
				Orange.Size = UDim2.new((stamina / 100), 0, 1, 0)
			end
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.