My script gets a different value from the current leaderstats

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to get the player’s current leaderstats Level value in runtime when the player touches a part, then let them enter if they have Level x or higher on their stats.

  2. What is the issue? Include screenshots / videos if possible!
    The script works fine until you change the value in the runtime. The script doesn’t detect the value change in runtime, but it does if you change it then start the game, which makes the code think that you still have, lets say, level 1, when your current leaderstats say level 2.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I didn’t find any questions related to this, so I wrote this post.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

This is the leaderstats code (in ServerScriptService):

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "leaderstats"
	print(3)
	local level = Instance.new("IntValue", leaderstats)
	level.Name = "Level"
	level.Value = 1
end)

Part.Touched script (Script that is on the part):

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local plr = game.Players:FindFirstChild(hit.Parent.Name)
		if plr:FindFirstChild("leaderstats").Level.Value == 2 then
			script.Parent.CanCollide = false
			wait(3)
			script.Parent.CanCollide = true
		else
		end
	end
end)

Make sure when you increase the Level.Value, you’re doing so on a server script and not a client script.


If that didn’t solve your issue, consider the following:

Are you able to confirm the .Touched event is being fired?

If so, at which if statement does your code stop?

If it stops at the one which checks the Level.Value while the Level.Value is 2, please print out what Level.Value is so we can get a better understanding of what the issue might be.

2 Likes

With server script do you mean server-side through an event or through a regular Script?

It can be either, it just cannot be changed from a local script as the client doesn’t have ownership over those values.

2 Likes

Thank you! It works perfectly now. I didn’t know clients didnt have ownership on stats.

1 Like