I want to kill the player if they make contact with an invisible box if they have a value over 1. The value is located at player.leaderstats.Stage.Value . I tried to look everywhere for things related to this, and I couldn't find anything. I'm not a very good scripter, so if you could, please explain what I could do to make this happen.
✓ ANSWER ACCEPTED
Kill player if they have certain value?
1 answer
✓ Accepted answer
Hello, one way to do this would be to connect .Touched to the part, .Touched gives a parameter for the part that touched the box, we can check if the part is a rig by checking if a humanoid exists in the character, if there is a humanoid, we know it's a rig, but to check and get the player, we can use GetPlayerFromCharacter to get it, then we just get the stage value and check if it's greater than 1, if it is, set the humanoid health to 0
local players = game:GetService("Players") -- This will be useful, and it is good practice to use GetService
local part = script.Parent -- Assuming this script is inside your invisible box
local valueThatKillsYouLol = 1 -- you can just change the name if you want, this is optional if you want more than 1 invisible box, we are gonna check if the stage value is greater than this (e.g 1)
part.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid") -- We use FindFirstChild because it returns nil if the child is not found, because we don't know if the part that touched is a character
if (humanoid) then
-- If it's a character
local player = players:GetPlayerFromCharacter(script.Parent) -- Getting the player
if (not player) then
-- If the character is a dummy or an npc
return -- don't go further than this line, this return will stop the code
end
local leaderstats = player:FindFirstChild("leaderstats")
if (leaderstats) then -- if the player's leaderstats are there and loaded
local stage = leaderstats:FindFirstChild("Stage")
if (stage) then
if (stage.Value >= valueThatKillsYouLol) then
-- If it's greater than the value to kill you
humanoid.Health = 0 -- Kill the player
end
end
end
end
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
What is the GetService use?