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!
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)
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.
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.
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