Need help with a script

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

Thank you for the help but it doesn’t work

Ok Ima try and re-write the code
One minute!

If this doesnt work ill have to help u later

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)
1 Like

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.

Because of the name… You gotta change it to the name of the Folder for the Stats

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.

The script was named according to the stats folder name which is “Balance”

Actually they guy above has a right script, he did an extra wait, so basically “FindFirstChild()” since because of the loading, it can’t find it.

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?

Script and put it into ServerScriptService

As a script inside ServerScriptService