i want to make some kind of defense game that kicks you out of the npc your defending dies,but i dont know how to do something like that nor can i find any tutorial to do something like that
This script shuld be instead the NPC not sure if it works
I Maybe have missed smthing out because usually in studio the script editor adds end and other things but when writing lua over here its hard.i cannot access the studio while doing this
If it didnt work add me on discord Agentgilspy Gaming#8624
local human = script.Parent.Humanoid while true do --This is a loop wait(1) if human.Health = 0 then -- checks if the NPC's is dead(heath = 0) for i, v in next, game:GetService"Players":GetPlayers() do v:Kick() end end end
Hey XxSpiritDuoXx
This is actually quite simple, all you are going to want to do is put a Script
into Server Script Service
and follow along with me as I guide you to success.
First things first, inside of your Script
we need to define Variables
. You should make a habit of declaring variables as they are a great way to organize your code. If you are unfamiliar with variables than I suggest that you read up on them at the wiki:
https://developer.roblox.com/en-us/articles/Variables
local NPC = game.Workspace:WaitForChild("NAME OF NPC")
Afterwards, let's go right ahead and create our function that detects when the NPC's health is changed.
local NPC = game.Workspace:WaitForChild("NAME OF NPC") NPC:FindFirstChild("Humanoid").HealthChanged:Connect(function(health) if health == 0 then for i, v in pairs(game.Players:GetChildren()) do v:Kick("You failed to protect the NPC, you're a failure and a disgrace to this country") end end end
Let's go over this code line by line. On line 3, we create a function that will activate when the NPC's humanoid health value is changed. Once it is, the script will then read through and perform what is in between the function and the end. Throughout your code, you can use the health argument which is located in the brackets at the end to reference the amount of health that the NPC has after it was changed. Keep in mind that you can name it to anything and it will always yield the same result. After that, we use an if statement to tell if the health value is 0 because if it is that means that the NPC has died and we need to kick the player. After that, we create a for loop that iterates through the player list and kicks each one of them. The reason I used a for loop that kicks everyone is that we don't actually know who the player is that we want to kick because this is a server script. If you want to get the specific player that failed then that is up to you to create that system.
Read up on the following wiki threads, they explain in more detail each thing we used to create this script:
https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged https://education.roblox.com/en-us/resources/intro-to-coding-coding-2-using-if/then-statements https://developer.roblox.com/en-us/articles/Loops