Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Player stat not changing?

I have a leaderboard where at first eveything worked out but then things started goiing wrong i here's my leaderboard

function oa(object)
local player = game.Players:playerFromCharacter(object)
if player ~= nil then
local ls = player.leaderstats
local sl = game.Workspace:FindFirstChild(ls.Stage.Value)
print("gah")
object.Torso.CFrame = object.Torso.CFrame + Vector3.new(0,3,0)
wait()
object.Torso.CFrame = sl.CFrame + Vector3.new(0,3,0)
end end

function oe(object)
if object.className == "Player" then
local ack = Instance.new("StringValue")
ack.Name = "leaderstats"
local ack2 = Instance.new("StringValue")
ack2.Name = "Stage"
ack2.Value = 1
ack2.Parent = ack
ack.Parent = object
end end

game.Players.ChildAdded:connect(oe)
game.Workspace.ChildAdded:connect(oa)

I tried using string value but it doesnt change the player's stage value

function ot(hit)
if hit.Parent ~= nil then
local player = game.Players:playerFromCharacter(hit.Parent)
if player ~= nil then
if player.leaderstats.Stage.Value == script.Parent.Name - 1 then
	print("Works")
local h = hit.Parent:FindFirstChild("Humanoid")
if h ~= nil then
if h.Health ~= 0 then
player.leaderstats.Stage.Value = script.Parent.Name
end end end end end end

script.Parent.Touched:connect(ot)

that's what gives you stage the print doesnt print so whats going on with it ?

you guys might say why not use int value it just doesnt work with my type of checkpoint system

1 comment

1

This is formatted terribly.

1 answer

✓ Accepted answer

The problem is that player.leaderstats.Stage.Value is a StringValue, and you compare its value (a string) to script.Parent.Name - 1 (which is a number). In Lua, a number will never be equal to a string (even if they represent the same number, for example print("1" == 1) will print false). Since you're only working with numbers, using an IntValue or NumberValue would be preferable, but if you're really sure you need a StringValue, you can use the tonumber function:

if tonumber(player.leaderstats.Stage.Value) == script.Parent.Name - 1 then

Also, digressing from the issue a bit, I would recommend that you indent your code. It makes it much easier to read, understand and find problems with missing end statements etc. Other ways to improve this besides indentation would be using the and statement, and using better variable/function names.

-- An example of improved code:
function OnTouched(hit)
	local player = game.Players:playerFromCharacter(hit.Parent)
	if player and tonumber(player.leaderstats.Stage.Value) == script.Parent.Name - 1 then
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid and humanoid.Health > 0 then
			player.leaderstats.Stage.Value = script.Parent.Name
		end
	end
end

script.Parent.Touched:connect(OnTouched)

(This blog post is good)

This discussion is preserved as it was. New answers, comments, and votes are closed.