Experience Incrementing Problem

I’m trying to increment my EXP, and it works the first time but after it just prints the same value over and over again, this is a script inside of a click detector Im just doing for testing

script.Parent.exp.MouseClick:Connect(function(plr)
	local Experience = _G.PlayerData[plr.UserId]["EXP"]
	Experience = Experience + 1
	print(Experience)
end)

you have to change the actual value of the player data or use a loop if you just want it to change for a certain part of your script
e.g.

for i=1,100 do
    local Experience = _G.PlayerData[plr.UserId]["EXP"]
	Experience = Experience + i
	print(Experience)
end

or you could do

_G.PlayerData[plr.UserId]["EXP"] = _G.PlayerData[plr.UserId]["EXP"] + 1
print(_G.PlayerData[plr.UserId]["EXP"])
1 Like

Wait why cant I use the variable I made, it’s still referring to the Data I need, I don’t get why it wouldnt work though

if you want to use that and save script effort just do this

local new = _G.PlayerData[plr.UserId]["EXP"] + 1
_G.PlayerData[plr.UserId]["EXP"] = new
print(new)
1 Like

Alright it’s fine ill use the first method you gave above thanks for the help!

1 Like

To explain, its likely because of the nature of _G as it is a module. As such when you set Experience locally, it copies the value from the module itself. As such to increment the through _G you’d need to set it using its reference. (_G.Whatever…).

1 Like