Trying to increase a value + 1

Im trying to make it so that, you touch a brick, it increases the value infinetly +1 everytime you touch it, but when I do, it doesnt increase its value. Here is the script below.

local XP = game.StarterGui.Player_Exp.XP_Num.Val.Value

script.Parent.Touched:Connect(function()
	XP = XP + 1
	script.Parent.BrickColor = BrickColor.new(255, 255, 255)
end)

Do I need to do

Anyway thank you!
4 Likes

By using XP in your code, you are modifying a static variable instead of the value you want. To fix this, use:

game:GetService("StarterGui").Player_Exp.XP_Num.Val.Value = game:GetService("StarterGui").Player_Exp.XP_Num.Val.Value + 1;

EDIT: Also, I would recommend using game:GetService() instead of dot syntax. I’ve fixed my code to compensate.

6 Likes

With this thing ; at the end of it?

1 Like

Not necessary. It’s just a habit.

1 Like

Do game.Players.PlayerGui.... instead of game.StarterGui...

1 Like

I hope that you do realize that you’re changing the value of the GUI that’s inside the StarterGui, not for the player’s gui.

(if you didn’t notice that atleast)

So the value will only change when the player dies (assuming the gui’s ResetOnDeath’s property is enabled.)

1 Like

What is the difference, I see people do it.

It’s a habit I carry over from other programming languages. In languages like Java, a semicolon is required at the end of every line due to it’s syntax.

1 Like

It’s a huge difference. StarterGui is what’s given when you join/your character spawns.

You want to do Player.PlayerGui in this case to directly change the value of the gui for per player. You also have to use a little bit of networking (remoteevents/ remote functions) to do this.

1 Like

I would not recommend throwing that in the ring because it can be easily exploited which this user shouldn’t have to deal with in that simple of a way. I would, instead, create a number value for every player who joined, storing it in the server storage (or just using a table), and then updating the XP there.

You are changing the value outside of the main event.

what you need to do is to change the value everytime it gets touched;

local XP = game.StarterGui.Player_Exp.XP_Num.Val.Value
script.Parent.Touched:Connect(function()
	XP = XP + 1
    game.StarterGui.Player_Exp.XP_Num.Val.Value = XP 
	script.Parent.BrickColor = BrickColor.new(255, 255, 255)
end)