Points Adding Back After Subtraction

So, I was having trouble with my leaderstats.

** What I am Trying To Achieve **

I am trying to achive a leaderboard system that you can use points to get past doors

** The Problem**

Basically how the points work is it uses a point per second system, but whenever you try to subtract the points from the door it just adds all the points back up again.

game.Players.PlayerAdded:connect(function(p)
local stats = Instance.new("IntValue", p)
	stats.Name = "leaderstats"
	local wins = Instance.new("IntValue", stats)
	wins.Name = "Wins"
local Points = Instance.new("IntValue", stats)
Points.Name = "Points"
local x = wins.Value
Points.Value = 0
while true do
wait(1)
Points.Value = Points.Value + 1 +  wins.Value
end
end)

** Lmk if you see any parts of my script that would be causing this problem, and I will be countinusly trying to solve this aswell.**

Can we see the script for the door?
also where you add the script you can do Points.Value += (1 + wins.Value) i believe

game.Players.PlayerAdded:Connect(function(p)
	
	local leaderstats = Instance.new("IntValue", p)
	leaderstats.Name = "leaderstats"
	
	local wins = Instance.new("IntValue", leaderstats)
	wins.Name = "Wins"
	
	local Points = Instance.new("IntValue", leaderstats)
	Points.Name = "Points"
	local x = wins.Value
	Points.Value = 0
	
	
	while true do
		Points.Value = Points.Value + 1 + wins.Value
		task.wait(1)
	end
end)

This isn’t a solution but I fixed your code up a bit. connect is deprecated and shouldn’t be used!

1 Like

It should be Connect not connect right?

The “C” needs to be capitalized, correct.

1 Like

You can also use self += 1 instead of self = self + 1

1 Like

This works, but whenever I enter the point door it subtracts it to go back to 0 and then just jumps right back up to whatever it was at.
Heres my local script if this is the problem

local Workspace = game:GetService("Workspace")
local door = Workspace.Door

door.Touched:Connect(function(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human ~= nil then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr.leaderstats.Points.Value >= door.Value.Value then
			script.Enabled = false
		plr.leaderstats.Points.Value = plr.leaderstats.Points.Value - door.Value.Value
		door.Transparency = 0.5
door.CanCollide = false
wait(1)
door.Transparency = 0
			door.CanCollide = true
			wait(5)
			script.Enabled = true
	end

	end
	end)

task.wait(1)

In this case you dont use task.wait() and you are just trying to be cool. Use wait() instead.

Points.Value = Points.Value + 1 + wins.Value

This is incorrect. Instead use:

Points.Value = Points.Value + 1 += wins.Value

Your making everything to simple.

Your using leaderstats as a name, it should be stats so it wont work.

Instead it should be like this:

local stats = Instance.new(“Folder”, p)
stats.Name = “leaderstats”

Lots of problems with your script. Dont attempt to fix peoples stuff if you dont know what your doing.

It a local script meaning it will not work
Common error but if you ever edit a server value via the local script then the server will still see it as it was before the local script did anything

My script has only one problem which was pointed out by @weding3.

  1. task.wait() is the improved version of wait(). It doesn’t throttle your code unlike wait()
  2. The mistake was already pointed out as I stated.

um the point about different adding is wrong it should be

Points.Value += 1 + wins.Value
1 Like

No? task.wait is the successor to wait and should be used instead.

This is incorrect. += can only be used when assigning a variable. This will give you a syntax error.

Roblox only recognizes leaderstats. And for a variable name, it doesn’t matter.

2 Likes

it does not matter about what the variable of the leaderstats is named as long as the folder itself is named ‘leaderstats’

You dont need to run events. It’s very simple to make this script.

I’m a little lost in this thread, but heading back to the original problem, you said the value keeps jumping back up correct? I presume the problem is related to this line of code being ran in a local script.

You need to change datastore values via the server and not the client. This is because it is local (in the name), while changing on the server will be a global change. I suggest finding some way to notify the server for when a user touches the door and change the value through a server script.

Multiple mistakes, you mean. I told you all of them.

Dude I found the error and why it’s not working

He is using variables, called FuncVrbs, which is short.

They only reconize stats. Thats why if you type leaderstats it would not display on the leaderboard.

task.wait() is used for pairs

NO he is not the problem is he is using A LOCAL SCRIPT FOR SERVER SCRIPTED VARIABLES.

I would recommend you stop replying as your replies aren’t contributive and spread false information.

Yes it does? I use this in my game and it works fine.

So I guess a Roblox Staff member is wrong?

2 Likes