Need help for simple code

Hello fellow developers,

I’m crafting a game with a straightforward mechanic where the player starts with an energy level of 100. As the player moves, the energy decreases by 5 points and jumping reduces it by 3 points. The current energy is displayed as text (Later on I will change it to a bar but for now Its just testing), for example, “95/100” after a move.

Should the energy level reach zero, the player will be unable to move or jump. To assist, there’s an icon that, when clicked, replenishes 5 energy points, allowing movement to resume.

I’m new to Lua and finding some aspects challenging. Any advice or suggestions would be greatly appreciated.
Thank you! :grin:

CEO_PixelZone

1 Like

You can use a while loop to check if they’re moving. I’d recommend taking a looking at the .MoveDirection of the Humanoid. You can also use .Jumping or the Humanoid State Changed event to monitor for jumps. You can have an int value for their energy level, and deduct energy every time they are moving. If they are out of energy, you can then set their WalkSpeed to 0.

Is there anything specific about this that you’re finding challenging so we can help you further?

if I got you correct this should work:

task.wait(.5)

local run = game:GetService("RunService")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local hum = char:WaitForChild("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")


local max_energy = 100
local min_energy = 0
local curr_energy = max_energy

local energy_depletion_amount = -3
local energy_augmentation_amount = 5

local function change_energy_amount(energy_change_amount)
	curr_energy += energy_change_amount
	
	if curr_energy < min_energy then
		curr_energy = min_energy
	elseif curr_energy > max_energy then
		curr_energy = max_energy
	end
end

local function update(dt)
	local moving = hum.MoveDirection.Magnitude -- you could also do hrp.AssemblyLinearVelocity.Magnitude
	
	if moving > 0 then
		-- moving
		
		change_energy_amount(energy_depletion_amount)
	else
		-- not moving
		
		change_energy_amount(energy_augmentation_amount)
	end
end

local function hum_state(old_state, new_state)
	if  new_state == Enum.HumanoidStateType.Jumping then
		change_energy_amount(energy_depletion_amount+2)
	end
end

hum.StateChanged:Connect(hum_state)
run.RenderStepped:Connect(update)

and if that is too fast then do this instead:

task.wait(.5)

local run = game:GetService("RunService")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local hum = char:WaitForChild("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")

local update_delay = 0.2

local max_energy = 100
local min_energy = 0
local curr_energy = max_energy

local energy_depletion_amount = -3
local energy_augmentation_amount = 5

local function change_energy_amount(energy_change_amount)
	curr_energy += energy_change_amount
	
	if curr_energy < min_energy then
		curr_energy = min_energy
	elseif curr_energy > max_energy then
		curr_energy = max_energy
	end
end

local function update(dt)
	local moving = hum.MoveDirection.Magnitude -- you could also do hrp.AssemblyLinearVelocity.Magnitude
	
	if moving > 0 then
		-- moving
		
		change_energy_amount(energy_depletion_amount)
	else
		-- not moving
		
		change_energy_amount(energy_augmentation_amount)
	end
end

local function hum_state(old_state, new_state)
	if  new_state == Enum.HumanoidStateType.Jumping then
		change_energy_amount(energy_depletion_amount+2)
	end
end

hum.StateChanged:Connect(hum_state)

while true do
	update()
	
	task.wait(update_delay)
end

hope this was helpful :slight_smile:

This is a simple code and works with many basics. Something you really should research out and learn yourself. A very good intro into all things programing. Here is a good start.