My script doesn't work

I have a script when you get up on a part you are given endurance but when I get up it doesn’t give me and gives an error

local Players = game:GetService("Players")
local playersInside = {}

script.Parent.Touched:Connect(function(hit)
	if hit.Name ~= "HumanoidRootPart" then return end

	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end

	local endurance = player:FindFirstChild("Endurance")
	local eMulti = player:FindFirstChild("eMulti")
	local class = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Class")

	if not endurance or not eMulti or not class or endurance.Value < 100 then 
		warn("Necessary values are missing or endurance is less than 100")
		return 
	end

	table.insert(playersInside, player)

	while table.find(playersInside, player) do
		task.wait(2)
		if not table.find(playersInside, player) then return end

		endurance = player:FindFirstChild("Endurance")
		if endurance then
			local endu = endurance.Value
			endurance.Value = endu + 1000 * tonumber(class.Value) * tonumber(eMulti.Value)
		else
			warn("Endurance not found during the loop")
		end
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Name ~= "HumanoidRootPart" then return end

	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end

	local playerIndex = table.find(playersInside, player)
	if playerIndex then
		table.remove(playersInside, playerIndex)
	end
end)
1 Like

error Workspace.LavaZone.Lava.Script:28: attempt to perform arithmetic (mul) on number and nil - Server - Script:28

I don’t know how to fix this, I tried a lot but it didn’t work.

.
One of them are not numbers, that’s why they are converted to nil by tonumber.

The class is not a number can you help fix the class I don’t want to make it a number

Then why are you trying to multiply it? Arithmetic operations only work with numbers.

Well, I need this, I multiplied all the factors, now the classes don’t work

Are they strings? You could try to cast them by linking to a number, depending on what you’re trying to achieve.

Yes, this is a string value. Can you help me fix it?

Is the string only a number or does it have other characters within it?

Do you mean about the class system?

Just make sure that the value is only a number without additional letters or random characters.

Listen, I have a class in my leaderstats, it is a string value

Can you print its value and tell us what it is? Switch it to this:

Add this before that line:

print(class.Value, eMulti.Value)

Workspace.LavaZone.Lava.Script:28: attempt to perform arithmetic (mul) on number and nil - Server - Script:28

Line 12 is the issue, also why did you alter the script so much and added a memory leak?

Switch it to this and tell me what it prints

Switch to this:

local endu = endurance.Value

print(class.Value, eMulti.Value)

endurance.Value = endu + 1000 * tonumber(class.Value) * tonumber(eMulti.Value)

Tell me what that print statement prints onto your console/output

I know the problem is in line 12 but this is an important line for me

Workspace.LavaZone.Lava.Script:31: attempt to perform arithmetic (mul) on number and nil - Server - Script:31