Is there something wrong with my script?

So, I’m learning (Experiment) about GUI Scripting, then I’m trying to make a simple stats upgrade, after I wrote some code and I run it, there is no error at all, but the code is not working at all,

This is my script, if there is an error, please correct the code:

-- ServerScript
game.Players.PlayerAdded:Connect(function(plr)
	local power = Instance.new("IntValue",plr)
	power.Name = "Power"
end)
-- Gui Script
local SG = game:GetService("StarterGui")
local button = SG.UpgradeGui.Frame.Frame.TextButton

button.MouseButton1Click:Connect(function(plr)
	local power = plr.Power
	plr.Power.Value = plr.Power.Value + 1
	if plr.Power.Value == 1 then
		print("Work")
	end
end)

Can you print the power’s value?

Nothing appears when I press the GUI Button, the print doesn’t work either

Also MouseButton1Click does not have a player parameter.

So, what function should I add? or i have to get player from variable?

Define the player as a variable with game.Players.LocalPlayer

It should throw an attempt index nil error if you tried to get the power intvalue object because plr is nil

1 Like

local SG = game:GetService("StarterGui") , notice that when a player is in-game, it won’t show him ‘StarterGui’, but ‘PlayerGui’.

Replace that line with :

`local Players= game:GetService("Players")
 local Player = Players.LocalPlayer
 local SG = Player:WaitForChild("PlayerGui")
 local Button = SG.UpgradeGui.Frame.Frame.TextButton

Button.MouseButton1Click:Connect(function()
	local power = Player:WaitForChild("Power")
	Player:WaitForChild("Power").Value  +=1
	if Player.Power.Value == 1 then
		print("Work")
	end
end)

2 Likes

I always forgets about this when making a ui.

So what should I do? add game.Players.LocalPlayer Variable or?

Also, I noticed you put Plr as a parameter inside the MouseButton1Click function,

Unlike a ClickDetector, you don’t need to put the player as a parameter there.

1 Like
  • I suggest you to use Remotes to add the player stats, it’s safer.
1 Like

Uh, you already declared the power object, no need to wait for it again;

2 Likes

in the server script add a folder named leaderstats U NEED TO NAME IT LEADERSTATS
and parent the int value to folder

normal script that creates the leaderboard:

-- ServerScript
game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	local Power = Instance.new("IntValue",leaderstats)
	Power.Name = "Power"

	
end)

local script inside the textbutton:

local rep = game:GetService("ReplicatedStorage")
local rem = rep:WaitForChild("RemoteEvent") 

local textbutton = script.Parent
textbutton.MouseButton1Click:Connect(function()
	rem:FireServer()
end)

the normal script that makes the player get more power when they press the button:

local rep = game:GetService("ReplicatedStorage")
local rem = rep:WaitForChild("RemoteEvent") 

rem.OnServerEvent:Connect(function(plr)
	local power = plr.leaderstats.Power
	power.Value = power.Value + 1 
end)

add a remote event in replicated storage

power.Value +=1

It’s always better to shorten.

Plus, he doesn’t have leaderstats, don’t confuse him.

2 Likes

I don’t want Power to be seen in the leaderstats

2 Likes

Have you tried what I offered? It’d be on the client only, but you can use remotes to fire the server.

2 Likes

Remote event with no sanity checks that increases a value is vunurable to exploits.

3 Likes
local RP = game:GetService("ReplicatedStorage")
local PR = RP:WaitForChild("PowerEvent")

PR.OnServerEvent:Connect(function(plr)
	plr.Power.Value = plr.Power.Value + 1
	if plr.Power.Value == 1 then
		print("Work")
	end
end)

Didn’t work either

`local Players= game:GetService("Players")
 local Player = Players.LocalPlayer
 local SG = Player:WaitForChild("PlayerGui")
 local Button = SG.UpgradeGui.Frame.Frame.TextButton

Button.MouseButton1Click:Connect(function()
	local power = Player:WaitForChild("Power")
	Player:WaitForChild("Power").Value  +=1
	if Player.Power.Value == 1 then
		print("Work")
	end
end)
2 Likes