Script adds values incorrectly

Code:

local CS = game:GetService("CollectionService")

local Config = {
	EasySkillStarVal = 0.025;
	MediumSkillStarVal = 0.05;
	HardSkillStarVal = 0.1;
}

local PlrsOnLaserCooldown = {}

local PlrsOnEasySkillStarCooldown = {}

local SkillStarCooldown = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvents"):WaitForChild("SkillStarCooldown")

for _, Laser in pairs(CS:GetTagged("Laser")) do
	
	Laser.Touched:Connect(function(hit)
		
		if hit.Parent:FindFirstChild("Humanoid") and not table.find(PlrsOnLaserCooldown,hit.Parent.Name) then
			
			hit.Parent.Humanoid.Health -= hit.Parent.Humanoid.MaxHealth
			
			table.insert(PlrsOnLaserCooldown,hit.Parent.Name)
			
			task.wait(0.5)
			
			table.remove(PlrsOnLaserCooldown,table.find(PlrsOnLaserCooldown,hit.Parent.Name))
			
		end
		
	end)
	
end

for _, JumpPad in pairs(CS:GetTagged("JumpPad")) do

	JumpPad.Pad.Touched:Connect(function(hit)
		
		if hit.Parent:FindFirstChild("Humanoid") then
			
			hit.Parent.Humanoid.JumpHeight = 33
			
			hit.Parent.Humanoid.Jump = true
			
			task.wait(0.1)
			
			hit.Parent.Humanoid.JumpHeight = 7.2
			
		end
		
	end)
	
end

for _, SkillStar in pairs(CS:GetTagged("EasySkillStar")) do
	
	SkillStar.Touched:Connect(function(hit)
		
		if hit.Parent:FindFirstChild("Humanoid") and not table.find(PlrsOnEasySkillStarCooldown,hit.Parent.Name) then
			
			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
			
			if plr then
				
				SkillStarCooldown:FireClient(plr,"Easy")
				
				plr.leaderstats.SP.Value += Config.EasySkillStarVal
								
				table.insert(PlrsOnEasySkillStarCooldown,hit.Parent.Name)

				task.wait(15)

				table.remove(PlrsOnEasySkillStarCooldown,table.find(PlrsOnEasySkillStarCooldown,hit.Parent.Name))
				
			end
			
		end
		
	end)
	
end

for _, SkillStar in pairs(CS:GetTagged("MediumSkillStar")) do

	SkillStar.Touched:Connect(function(hit)

		if hit.Parent:FindFirstChild("Humanoid") and not table.find(PlrsOnEasySkillStarCooldown,hit.Parent.Name) then

			local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

			if plr then

				SkillStarCooldown:FireClient(plr,"Medium")

				plr.leaderstats.SP.Value += Config.MediumSkillStarVal

				table.insert(PlrsOnEasySkillStarCooldown,hit.Parent.Name)

				task.wait(15)

				table.remove(PlrsOnEasySkillStarCooldown,table.find(PlrsOnEasySkillStarCooldown,hit.Parent.Name))

			end

		end

	end)

end

Whenever I try to add the skillstar value from the config it gives me a number such as 0.0250000002 or 0.02500000001

It’s a normal floating point error which you can’t control, so you might try rounding it with something like this:

local rounded = math.round((plr.leaderstats.SP.Value + Config.EasySkillStarVal)*1000) -- 25 (if the leaderstats value was 0)
plr.leaderstats.SP.Value = value / 1000 -- 0.025

This should work for the other Skill Stars as well, just keep in mind that if you ever make any value in the config less than 0.001 then you will have to change 1000 to 10000, 100000 etc.

This makes sure that whatever goes inside math.round() is always like 25.00000000001 and NOT 2.500000001 because that would end up being 0.0002

Hope this helps! :smile:

1 Like

Hey, so the issue that I talked abt has solved but another has risen. Now every time a player collects the skill stat they get like billions SP even though there is a cooldown

Even if the cooldown doesn’t work, with your current config values, it would take tens of billions of additions to get that large value of an SP. Hurray, it’s debugging time! :sob:

Anyways, I suggest keeping track of How many times the connected function to the touched event is activated as well as printing every time the SP is increased. Let me know you can track down an error…

Are you sure that there aren’t any duplicate threads in the background that you forgot to terminate? It happens to me too.

I debugged a little yesterday and found out that it starts correctly at 0.025 but increases for some reason

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