Hi, what I’ve been trying to do was making a script that makes a GUI in StarterGUI and kills a player when they reach 0 or less points in the leaderboard. But it doesn’t seem to work. Did I do anything wrong?
local function killPlayer(player)
player.Character:BreakJoints()
end
local function checkPoints(player)
local pointsValue = player.leaderstats.Balance
if pointsValue.Value <= 0 then
killPlayer(player)
end
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
checkPoints(player)
end)
end)
for _, player in ipairs(game.Players:GetPlayers()) do
checkPoints(player)
end
The problem is this only runs once.
So once they do lose all there points it wont run!
I have a solution though!
local function killPlayer(player)
player.Character:BreakJoints()
end
local function checkPoints(player)
local pointsValue = player.leaderstats.Balance
pointsValue:GetPropertyChangedSignal('Value'):Connect(function()
if pointsValue.Value <= 0 then
killPlayer(player)
end
end
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
checkPoints(player)
end)
end)
for _, player in ipairs(game.Players:GetPlayers()) do
checkPoints(player)
end
This should make it so when the value changes, if it is less then or equal to 0 it will kill them!
Hope this works!
PS if it doesnt I can re-write the code later! I didnt write this in Roblox so there could be errors
local function killPlayer(player) -- kills the player
player.Character:BreakJoints()
end
local function checkPoints(player) -- check the value in the player
local pointsValue = player.leaderstats.Balance
print(pointsValue.Value)
if pointsValue.Value <= 0 then
killPlayer(player)
end
end
game.Players.PlayerAdded:Connect(function(player)
local pointsValue = player.leaderstats.Balance
player.CharacterAdded:Connect(function(character)
checkPoints(player)
pointsValue:GetPropertyChangedSignal('Value'):Connect(function() -- check if the value changes
checkPoints(player)
end)
end)
end)
for _, player in ipairs(game.Players:GetPlayers()) do -- loop through the players and check!
local pointsValue = player.leaderstats.Balance
checkPoints(player)
pointsValue:GetPropertyChangedSignal('Value'):Connect(function() -- check if the value changes!
checkPoints(player)
end)
end
Also some questions involving this if it does not work.
A: Any errors?
B: What kind of value is the points value? Number, string, etc.
C: Are you changing the value of the points via a server script or local script?
D: Is a number being printed into the output?(I made it so when the value changes it will print it into the output)
If it does not work please answer these questions so I can help!
You don’t need to do this, as every time the player character spawns a function (Connect) will be added and the output will be an unnecessary overhead and possibly errors.
It needs to be added only when the player enters
game.Players.PlayerAdded:Connect(function(player)
local pointsValue = player:WaitForChild("leaderstats"):WaitForChild("Balance")
pointsValue.Changed:Connect(function()
checkPoints(player)
end)
end)
What is the point? When they got 0 or less points in the leaderboard, they get killed and a GUI gets added? For what reason, and I don’t understand it.
What I’m trying to make is a tycoon game where it makes the player lose the game if they reach bankruptcy (having 0 or negativity in leaderpoints). When they reach 0, the player will be killed and a UI will pop up stating that the game is over.
local function killPlayer(player)
player.Character:BreakJoints()
end
local function checkPoints(player)
pcall(function()
local pointsValue = player:FindFirstChild("leaderstats"):FindFirstChild("Balance")
if pointsValue and pointsValue.Value < 0 then
killPlayer(player)
end
end)
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
checkPoints(player)
end)
end)
for _, player in ipairs(game.Players:GetPlayers()) do
checkPoints(player)
end
I don’t know if this works, but the code seems correct.
This doesn’t work for me. Is the script supposed to be a local script or script? and where must the script be located at in order to make it functional?