What in the world is going on rn?

So like I decided to make a healthbar, and chakra bar. The Chakra bar would be using attributes right? Well there’s some weird bug going on where when the player loses chakra, it prints the stat going down HOWEVER the bar makes it look like theyre losing chakra. WHATS GOING ON ROBLOX!?!!

My Code:

repeat
	task.wait()
until game:IsLoaded()

local Player = game.Players.LocalPlayer
local Character
local Humanoid
Character = Player.Character or Player.CharacterAdded:Wait()
Humanoid = Character:WaitForChild("Humanoid")
Player.CharacterAdded:Connect(function(NewCharacter: Instance)
	Character = NewCharacter
	Humanoid = NewCharacter:WaitForChild("Humanoid")
end);

local PlayerGui = Player.PlayerGui


while task.wait() do
	if Character and Humanoid then
		local StatBars = PlayerGui:FindFirstChild("StatBars")
		local Health, Chakra = StatBars:FindFirstChild("HealthBar"), StatBars:FindFirstChild("ChakraBar")
		local HealthBg, ChakraBg = StatBars:FindFirstChild("Health"), StatBars:FindFirstChild("Chakra")
		if Health and Chakra then
			local ChakraAmount, MaxChakra = Character:GetAttribute("Chakra"), Character:GetAttribute("MaxChakra")
			if ChakraAmount and MaxChakra then
				print(ChakraAmount, (ChakraAmount/MaxChakra))
				Chakra.Size = UDim2.new(ChakraBg.Size.X.Scale/(ChakraAmount/MaxChakra), 0, ChakraBg.Size.Y.Scale, 0)
				Health.Size = UDim2.new(HealthBg.Size.X.Scale/(Humanoid.Health/Humanoid.MaxHealth), 0, HealthBg.Size.Y.Scale, 0)
			end
		end
	end
end;

Result from the code:
https://gyazo.com/a0354ec1b9870e78d0f60b7c27530677

Surely if it’s going forward then your mathematic equation calculating the size of the bar is wrong, it’s probably dividing the scale of ChakraBg by a decimal figure < 1.

For example

If ChakraBg Size is 1
Chakra amount is 20
Chakra max is 100

1/(20/100) is equivalent to 1/0.2 which would equal 5, thus being 5 times the size of the bar.

Maybe your equation is scuffed

1 Like

But it works for the health calculation. I’ve tested that, and if you look at the code it’s the same thing.

Do you have any other code/scripts anywhere else that could be affecting the Chakra amount? It looks like you might have a loop somewhere else that is increasing that value constantly.

1 Like

As a matter of fact I do good sir. I will show you, but know this I printed the amount on client / server and the results shows that it is as a matter of fact decreasing.

Code:

local Tool = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Events = ReplicatedStorage:WaitForChild("Events")
local VFX = Events:WaitForChild("VFX")

local Debounce = false
local RasenganTimer = 10

function ActivatedRasengan()
	if Tool.Parent:FindFirstChild("Humanoid") and not Debounce then
		Debounce = true
		local Character = Tool.Parent
		local Info = {
			Effect = "Rasengan",
			Parent = Tool.Parent
		}
		VFX:FireAllClients(Info)
		
		local activationTime = tick()
		local drainchakraTimer = tick()
		
		coroutine.wrap(function()
			while task.wait() do
				if tick() > activationTime + 8 then
					break
				else
					if tick() > drainchakraTimer + .25 then
						drainchakraTimer = tick()
						local ChakraAmount = Character:GetAttribute("Chakra")
						if ChakraAmount and ChakraAmount > 0 then
							local ChakraLoss = ChakraAmount-2
							Character:SetAttribute("Chakra", ChakraLoss)
							local NewChakra = Character:GetAttribute("Chakra")
							print(NewChakra)
						end
					end
				end
			end
		end)()
		
		task.wait(RasenganTimer)
		Debounce = false
	end
end
Tool.Activated:Connect(ActivatedRasengan)

This is very odd - Logically I think it looks like it checks out. What happens if you manually change the players health from the server-side when in test mode?

My only best guess at this point is similar to what @Zek4ry mentioned, which would be an issue with the division of the bar’s X scale. Keep in mind scale only goes between 0-1, with 1 being “max size of the parent element”. Pulling some fake numbers out of thin air, that would look like:

--Original logic
Chakra.Size = UDim2.new(ChakraBg.Size.X.Scale/(ChakraAmount/MaxChakra), 0, ChakraBg.Size.Y.Scale, 0)

-- Assume ChakraBG.Size.X.Scale = 1, ChakraAmount = 5, and MaxChakra = 20
(ChakraBg.Size.X.Scale/(ChakraAmount/MaxChakra) = (1 / (5 / 20)) = 4

Since you’re passing in those values on the Scale parts of the UDim2, that means your scale would then be 4 times bigger than the max size of the parent element of that GUI, which could possibly explain why its growing instead of shrinking.