How do you do this?

So basically, I want to set the requirement value without changing the level value.
Sounds confusing right?

As you can see, the level value is assuming the value of the requirement variable, which I don’t want it to do.
And for some reason, there’s a 50% chance of this happening which makes it even weirder
What the hell is going on

game.Players.PlayerAdded:Connect(function(plr)
	local Requirement = plr:WaitForChild("Levels"):WaitForChild("Requirement")
	local level = plr:WaitForChild("Levels"):WaitForChild("Level")
	local levelvalue = plr:WaitForChild("Levels"):WaitForChild("Level").Value
	local exp = plr:WaitForChild("Requirement"):WaitForChild("Exp")
	Requirement.Value = levelvalue * 25
	print(plr:WaitForChild("Levels"):WaitForChild("Requirement").Value)
	if exp.Value > Requirement.Value then
		level.Value +=1
		Requirement.Value = levelvalue * 25
		exp.Value = 0
	elseif exp.Value == Requirement.Value then
		exp.Value = exp.Value - Requirement.Value
		level.Value +=1
		Requirement.Value = levelvalue * 25
end
	exp.Changed:Connect(function()
		if exp.Value > Requirement.Value then
			level.Value +=1
			Requirement.Value = levelvalue* 25
			exp.Value = 0
		elseif exp.Value == Requirement.Value then
			level.Value +=1
			exp.Value = exp.Value - Requirement.Value
			Requirement.Value = levelvalue * 25
		end
	end)
end)

I don’t know if this is it but I think it’s because you didn’t add a parameter for the exp.Changed

What parameter would even be for exp.Changed?
I’m honestly really confused and fed up with scripts just not working as they should
Why is it changing the value, I’ve tried everything

You might be getting a race condition with the changed event changing itself.
Each time the exp.Value is changed it triggers a new event, depending on how these are scheduled to run they might be running before the values have been updated elsewhere.

Try moving all theexp.Value = something parts of your code to the end of their codeblocks.

1 Like

It looks like you’re trying to detect if the value of exp is changed to know when you’re supposed to level up, so I believe you just need to use exp.Changed:Connect(function(Value)

1 Like

The problem is you are not updating the levelvalue variable whenever you change the level of the player.

2 Likes

I’m not sure what you quite mean by moving it to the end of the code block
I’ve tried doing what you said and it hasn’t worked
Did I do it wrong?

game.Players.PlayerAdded:Connect(function(plr)
	local Requirement = plr:WaitForChild("Levels"):WaitForChild("Requirement")
	local level = plr:WaitForChild("Levels"):WaitForChild("Level")
	local levelvalue = plr:WaitForChild("Levels"):WaitForChild("Level").Value
	local exp = plr:WaitForChild("Requirement"):WaitForChild("Exp")
	Requirement.Value = levelvalue * 25
	print(plr:WaitForChild("Levels"):WaitForChild("Requirement").Value)
	--if exp.Value >= Requirement.Value then
	--	level.Value +=1
	--	exp.Value = exp.Value - Requirement.Value
	--	Requirement.Value = levelvalue * 25
	--end
	exp.Changed:Connect(function()	
		if exp.Value >= Requirement.Value then
			level.Value +=1
Requirement.Value = levelvalue * 25
			exp.Value = exp.Value - Requirement.Value
		end
	end)
end)

Nope, didn’t quite work either
I made the levelvalue variable as for some reason, those types of variables never seem to get updated

Now the script is beginning to act up a lot more when I use value instead of exp.Value

No, it’s really because you are not updating the variable so the code assumes that the level of the player is still the same as previous.

This is what the code should look like:

game.Players.PlayerAdded:Connect(function(plr)
	local Requirement = plr:WaitForChild("Levels"):WaitForChild("Requirement")
	local level = plr:WaitForChild("Levels"):WaitForChild("Level")
	local levelvalue = plr:WaitForChild("Levels"):WaitForChild("Level").Value
	local exp = plr:WaitForChild("Requirement"):WaitForChild("Exp")
	Requirement.Value = levelvalue * 25
	print(plr:WaitForChild("Levels"):WaitForChild("Requirement").Value)
	if exp.Value > Requirement.Value then
		level.Value +=1
		levelvalue = level.Value
		Requirement.Value = levelvalue * 25
		exp.Value = 0
	elseif exp.Value == Requirement.Value then
		exp.Value = exp.Value - Requirement.Value
		level.Value +=1
		Requirement.Value = levelvalue * 25
    end
	exp.Changed:Connect(function()
		if exp.Value > Requirement.Value then
			level.Value +=1
			levelvalue = level.Value
			Requirement.Value = levelvalue* 25
			exp.Value = 0
		elseif exp.Value == Requirement.Value then
			level.Value +=1
			exp.Value = exp.Value - Requirement.Value
			Requirement.Value = levelvalue * 25
		end
	end)
end)

I hope this helps!

1 Like

image
At first it levelled me up by 10 more levels than it should have, worked properly and then all of a sudden I was level 701
It’s definitely an improvement from before but not quite there

Oh yeah, I forgot to look at how you negate xp per level up. You did the xp negation wrong. Instead it should be more like this:

if exp.Value >= Requirement.Value then
	level.Value += 1
	levelvalue = level.Value
	exp.Value -= Requirement.Value
	Requirement.Value = levelvalue * 25
end

You don’t need to add the elseif statement because it does the same thing in the if statement above. This method will keep the exp value and not reset it.

I hope this helps out!

Edit: The second code sample is quite wrong so I decided to remove it.

1 Like

Thank you so much!
Works quite well but I’ll do further tests just incase before marking it as a solution

1 Like

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