Advanced problem (Values etc.)

Hi,
I added to my tycoon battery that open door when it’s 0% and it can be charged by click (+10%) and upgrader for this battery (2x bigger battery life) but when i upgrade the battery nothing works…

script.Parent.ClickDetector.MouseClick:Connect(function()
	if script.Parent.Value.Value >= 90 then
		script.Parent.Value.Value = 100
		script.Parent.BillboardGui.TextLabel.Text = script.Parent.Value.Value.."%"
	else
		script.Parent.Value.Value += 10
		script.Parent.Transparency = 0
		script.Parent.Material = "Neon"
		script.Parent.BillboardGui.TextLabel.Text = script.Parent.Value.Value.."%"
		workspace.WO.BaseDoorWO.CanTouch = true
		workspace.WO.BaseDoorWO.Transparency = 0.8
	end
end)

while wait(1) do
	if workspace.WO.Owned.Value == true and script.Parent.Value.Value > 0 then
		if script.Parent.Gamma.Value == true then
			script.Parent.Value.Value -= 0.5
			script.Parent.BillboardGui.TextLabel.Text = script.Parent.Value.Value.."%"
		else
			script.Parent.Value.Value -= 1
			script.Parent.BillboardGui.TextLabel.Text = script.Parent.Value.Value.."%"
		end
	elseif script.Parent.Value.Value == 0 then
		script.Parent.Transparency = 0.8
		script.Parent.Material = "Glass"
		workspace.WO.BaseDoorWO.CanTouch = false
		workspace.WO.BaseDoorWO.Transparency = 1
	end
end

Gamma is upgraded battery name

I haven’t fully read your code, but do know that you can store values in variables right? This can make your code much easier to read and write. This would look like this:

local Parent = script.Parent -- the parent of the script
local Value = Parent.Value -- the number value
local Gamma = Parent.Gamma
local TextLabel = Parent.BillboardGui.TextLabel -- your text label

local WO = workspace.WO -- The base I think?
local DoorWO = WO.BaseDoorWO -- The base door
local Owned = WO.Owned -- the bool value

script.Parent.ClickDetector.MouseClick:Connect(function()
	if Value.Value >= 90 then
		Value.Value = 100
		TextLabel.Text = Value.Value.."%"
	else
		Parent.Transparency = 0
		Parent.Material = "Neon"

		Value.Value += 10
		TextLabel.Text = Value.Value.."%"

		DoorWO.CanTouch = true
		DoorWO.Transparency = 0.8
	end
end)

while wait(1) do
	if Owned.Value == true and Value.Value > 0 then
		if Gamma.Value == true then
			Value.Value -= 0.5
			TextLabel.Text = Value.Value.."%"
		else
			Value.Value -= 1
			TextLabel.Text = Value.Value.."%"
		end
	elseif Value.Value == 0 then
		Parent.Transparency = 0.8
		Parent.Material = "Glass"
		DoorWO.CanTouch = false
		DoorWO.Transparency = 1
	end
end

Also, how is your system set up? Can you show a screenshot of the objects in the explorer? Also, how are you deciding how much power the battery can store? I don’t see anything in your code that limits it. Can you explain the variables?

1 Like

It still don’t works… thanks for time spend on this

It isn’t supposed to work. I didn’t change any of the functioning of the script. All I did was add variables so it’s easier to read. I remarked below the script that you need to provide more information about what values do what. Also, can you only paste the specific segment of code where you are trying to make the door store more power? I currently can’t tell where in your code you are attempting to do this because I don’t understand what each value/objects purpose is.

1 Like

Wait… IntValue can only store natural numbers?

IntValue should only be used for whole numbers. Use NumberValue if you’d like to integrate any decimal values. I’d suggest that if you’re using a NumberValue, to also wrap it in math.floor.

That’s the thing. We have no idea if it is an IntValue | Roblox Creator Documentation or a NumberValue | Roblox Creator Documentation. That’s why I said we need to know what each object is and what it’s purpose in the code is.

Also,

what you said said here doesn’t make sense, as it’d defeat the whole point of the number values in the first place. The number values is meant to be able to store doubles, or floats, so using math.floor on it would make no sense and defeat the purpose of having it.

usage of math.floor ~= whole number

e.g:

math.floor(3.98372 / 0.1 + 0.5) * 0.1 --> 3.9

I think he means to use math.floor so that you don’t have the common:

numValue.Value = 5

print(numValue.Value) --> 5.000000001

issue

I was basing it off of what he said, which is all I know. He said something about percentages. So if he does have any decimals, he’ll need to multiply it and wrap it in a math.floor to get the percentage.

Yes I also was referring to that common issue.

Any errors in output did u tried printing where your script stops etc.? You didn’t give us much informations.

any error?
as this would help also mark error line

That isn’t how math.floor works. math.floor basically remove the decimal value of a number. Quoted directly from the math | Roblox Creator Documentation page it, “Returns the largest integer smaller than or equal to x.” You can think of it as rounding down to an integer. Here is what it’d do:

print(math.floor(3.9) == 3) -- true because math.floor(3.9) returns 3.

The inverse of math.floor, which is math.ceil basically rounds up to the nearest integer. This can be seen by this:

print(math.ceil(3.01) == 4) true because math.ceil(3.01) returns 4

He was saying when it had 0% charge. He never said he was actually retrieving the percentages and I see nothing in his code that indicates otherwise.

Alright, well based on what he says, I assumed he would be displaying the battery percentage somewhere. If that’s the case, that’s what he’d use math.floor for. For example:
print(math.floor(.3248284823 / 1 * 100))
prints 32 which is 32%.

Notice how after the floor it is multiplied by 0.1?

He is, but not in that way. I understand what you are trying to say though. He is instead taking the value directly and concatenating it with the % sign.