Your script checks Materials Value just once at the beginning. You should make a loop to check for the materials periodically:
local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
local CHECK_INTERVAL = 5
while true do
wait(CHECK_INTERVAL)
if plr.leaderstats.Materials.Value < 0 then
plr:Kick("You have gone bankrupt in materials!")
end
end
It is however discouraged, as roblox has special value change listeners, which you should use instead:
local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
plr.leaderstats.Materials.Changed:Connect(function()
if plr.leaderstats.Materials.Value < 0 then
plr:Kick("You have gone bankrupt in materials!")
end
end)
Better still, even if there is one player allowed, you should put script like this on server, to combat potential future exploiters. Following script should be in ServerScriptService:
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local materials = Instance.new("IntValue")
materials.Name = "Materials"
materials.Value = 0
materials.Parent = leaderstats
materials.Changed:Connect(function()
if materials.Value < 0 then
plr:Kick("You have gone bankrupt in materials!")
end
end)
end)
P.S. My personal opinion is that you should not ever kick players for things like this. They will be discouraged from joining Your game again. Is it not better to simply respawn character?
1 comment
The error: "Players.Anotherperson_999.Backpack.BankruptKick:3: attempt to index local 'plr' (a nil value)" keeps coming up when i try them.