GUI Button that when clicked decreases leaderstats.Health

Hello Developers!
I’m trying to make a leaderstats IntValue that decreases when clicked on a UI Button.
I’ve tried something like this:

script.Parent.MouseButton1Click:connect(function(player)
	game.Workspace.Wrong:Play()
	player.leaderstats.Health.Value = player.leaderstats.Health.Value - 1
end)

I know that leaderstats never exist when calling the event.
But, I’ve only gotten errors like this in the output:

I’m making a Health Value that decreases when clicked on the wrong value. If that helps explain anything.

This is the leaderstats script that I used:

function onPlayerEntered(newPlayer)
	wait(.5)
	local stats = Instance.new("IntValue")
	stats.Name = "leaderstats"

	local score = Instance.new("IntValue")
	
	score.Name = "Health"
	score.Value = 4

	score.Parent = stats	
	stats.Parent = newPlayer
end

game.Players.ChildAdded:connect(onPlayerEntered)

Extra Note: I know that LocalScripts only affect LocalPlayers. But, how do I make it so that when I click the GUI Button it affects leaderstats? Maybe RemoteEvents?

Trust me! I have tried to find something close to this here on the DevForum but to no avail. I found nothing. Thank you for reading!

2 Likes

Hey, if you were to do the following, I don’t believe it would actually give you the player:

script.Parent.MouseButton1Click:Connect(function(player)
     print(player)
end

If you want other players to see the persons leaderstats going down, you’re gonna have to put a RemoteEvent in there somewhere. Try the following.

-- client script
local plr = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	game.Workspace.Wrong:Play()
	game.ReplicatedStorage.RemoteEvent:FireServer()
end)
-- server script
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr)
	plr.leaderstats.Health.Value = plr.leaderstats.Health.Value - 1
end)
2 Likes

I see lots of things wrong.

First off, you’re decreasing the health in a local script, which is easily exploitable. You’ll need to use a RemoteEvent and handle the health decrease on the server.

Create a remote event called “HealthDecrease” and place it in ReplicatedStorage.
UI Button script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
script.Parent.MouseButton1Click:Connect(function(player)
	game.Workspace.Wrong:Play()
	ReplicatedStorage.HealthDecrease:FireServer() -- Fires the RemoteEvent you created that in ReplicatedStorage when the button is clicked
end)

As for your leaderstats script, you’re making the leaderstats an IntValue instead of a Folder. You’re also called the “onPlayerEntered” function as ChildedAdded instead of PlayerAdded.

leaderstats script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function onPlayerEntered(player)
	local leaderstats = Instance.new("Folder") -- the leaderstats needs to be a folder, not an IntValue.
    leaderstats.Parent = player
	leaderstats.Name = "leaderstats"

	local Health = Instance.new("IntValue")
	Health.Parent = leaderstats	
	Health.Name = "Health"
	Health.Value = 4

end

ReplicatedStorage.HealthDecrease.OnServerEvent:Connect(function(player)
     player.leaderstats.Health.Value -= 1 -- When the RemoteEvent is fired, the health decreases by one.
end)

Players.PlayerAdded:Connect(onPlayerEntered)
1 Like

First of all, thank you so much sorify. I have marked your post as a solution post. Valiant also deserved it as well. But, Valiant’s leaderstats did not work, but his advice among with sorify’s script made me achieve a solution.

W2

W3

Note: It did work, but there are errors and nothing pops up in my LocalPlayer.


Overall, thank you so much for both of your help. You two are very helpful and have given me support. God bless you both! Have a great day! Sorry for making this big post as well. Just wanted to note on some stuff. :happy1:

1 Like

Why is that leaderstats folder parented to the workspace container?

You should be doing leaderstats.Parent = player not leaderstats.Parent = workspace.

From a quick glance the scripts provided here look fine.

1 Like

I see the issue, you’re missing a closing bracket in the end statement above the underlined text.

Yes, I edited my reply when he pointed out my error a few hours ago.

local player = script.Parent.Parent.Parent.Parent 
function onclick()
player.leaderstats.Health.Value = player.leaderstats.Health.Value - 1
end
 
script.Parent.MouseButton1Click:connect(onclick)

--server script in button