Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Hey guys, back for help. Why isn't my kick script working when you go bankrupt in materials?

Asked by 4 years ago

Hey people. I am making another prototype called "Town simulator", i want it to kick you when you go below 0 materials but its not working. I put it as a regular script in StarterPack and the servers are 1 person only. If you want to test it play: https://web.roblox.com/games/3255053604/Tycoon-BETA

local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
  if plr.leaderstats.Materials.Value < 0 then
      plr:Kick("You have gone bankrupt in materials!")
  end
  

1 answer

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
4 years ago
Edited 4 years ago

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 --every 5 seconds for example
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)

    --insert your leaderstats code here
    --code below is my best guess only
    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?

0
The error: "Players.Anotherperson_999.Backpack.BankruptKick:3: attempt to index local 'plr' (a nil value)" keeps coming up when i try them. AnotherPerson_999 28 — 4y
Ad

Answer this question