My player dosnt running!

Hello my guys, my player dosnt running and i dont know what the problem!
The peculiarity of my script is that it takes information about stamina from the player himself in the PlayerStats folder, but for some reason nothing happens
Another script creates an IntValue inside the player called Stamina and its default value is 500.

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")

local function updateStamina(player)
	local playerStats = player:WaitForChild("PlayerStats")

	if playerStats then
		local stamina = playerStats:WaitForChild("Stamina")

		if stamina then
			local currentStamina = stamina.Value

			if currentStamina > 0 then
				local walkSpeed

				if currentStamina >= 400 then
					walkSpeed = 23
				elseif currentStamina >= 300 then
					walkSpeed = 21
				elseif currentStamina >= 200 then
					walkSpeed = 19
				elseif currentStamina >= 100 then
					walkSpeed = 16
				else
					walkSpeed = 14
				end

				if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) then
					player.Character:FindFirstChildOfClass("Humanoid").WalkSpeed = walkSpeed
					stamina.Value = currentStamina - 5
				else
					player.Character:FindFirstChildOfClass("Humanoid").WalkSpeed = 14
				end

				stamina.Value = math.min(currentStamina + 1, 500) 
			else
				player.Character:FindFirstChildOfClass("Humanoid").WalkSpeed = 14
			end
		else
			warn("Stamina not found for player", player.Name)
		end
	else
		warn("PlayerStats not found for player", player.Name)
	end
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local playerStats = player:WaitForChild("PlayerStats", math.huge) 

		if playerStats then
			local stamina = playerStats:FindFirstChild("Stamina")

			if stamina then
				while wait(1) do
					updateStamina(player)
				end
			else
				warn("Stamina not found for player", player.Name)
			end
		else
			warn("PlayerStats not found for player", player.Name)
		end
	end)
end)
1 Like

image

I realized that I’m doing it wrong guys

I inserted the script into the startercharacter but nothing changed

I see that you placed warning messages in the script. Is there any warning message showing up in the output window?

I think it might be better to use the UserInputService:InputBegan event insted of IsKeyDown. When the function updateStamina() is called, it checks only one time if the key is being held down. Just use a debounce and UserInputService:InputBegan and UserInputService:InputEnded. I don’t think this will fix the problem but it can make the script better.

Needs to be a local script, that looks like a server script, which will not work for this.

Also, use a LocalScript instead of a Server Script. UserInputService is a client-side service.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.