Kill On Touched

I would like to script something so that if you touched an object and you don’t have a certain gamepass it killed you but I have no idea what I would do for that, I would appreciate your help.

Step one: Create a script into the part.
Step two: This is the code.

local part = script.Parent

part.Touched:Connect(function(plr)
local Humanoid = plr:FindFirstChild(“Humanoid”)
if Humanoid then
Humanoid.Health = 0
end
end)

me not knowing how to code blocks

1 Like

How would I do it only if they don’t own the gamepass?

You would use MarketplaceService, I suppose. Let me write a script real quick.

You could change that to a function that the humanoid has which is :TakeDamage()

edit: the player doesn’t have a humanoid, the character does.

so the script would be:

local part = script.Parent


part.Touched:Connect(function(plr)
local Humanoid = plr.Character:FindFirstChild(“Humanoid”)
if Humanoid then
Humanoid:TakeDamage(100)
end
end)

You would use UserOwnsGamePassAsync and .Touched. Here’s an example!

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local Part = script.Parent --Put the part you want to kill when they touch here.
local GamePassId = 0 --Put the gamepassid here.

Part.Touched:Connect(function(Hit)
    local Character = Hit.Parent
    local Humanoid = Character and Character:FindFirstChild("Humanoid")
    local Player = Character and Players:GetPlayerFromCharacter(Character)

    if Player and Humanoid and not MarketplaceService:UserOwnsGamePassAsync(Player.userId, GamePassId) then
        Humanoid.Health = 0
    end
end)
2 Likes

Oh, thanks.

ignore this. Stupid 30 character limit.

1 Like

I did it correctly in my post. Please read before you reply like that. :slight_smile:

1 Like

Apologies, was in the middle of writing it when you posted it, didn’t see it I’ll retract the post

Thank you. :slight_smile: And thank you @popeeyy

An alternate way of writing it would be like this, I have commented the code so you understand it, but the method is very similar to how popeeyy did it:

--Variables:

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")


--If something touches the part:
script.Parent.Touched:Connect(function(hit)
	
	
	--If a humanoid ie a character touches the part:
	if hit.Parent:FindFirstChild("Humanoid") then
		
		--Get the roblox player from the character:
		local Player = Players:GetPlayerFromCharacter(hit.Parent)
		
		--Check if the playerid has the gamepass, if they dont (~= true):
		if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GAMEPASSIDHERE) ~= true then
		--Then get the character of the player and make their health 0:
		Player.Character.Humanoid.Health = 0
		
		else
			
		--Whatever you want to happen if they do own the gamepass.
		
		end
			
	end
	
	
end)

@popeeyy’s solution should work however and if it does work for you then please do mark that as the solution.

I added that script but it doesn’t seem to work, does it have to be a local script? image

For a seat, you should use :GetPropertyChangedSignal on the Occupant instead of .Touched. Here’s a script that would eject them from their seat:

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local Seat = script.Parent --Put the part you want to kill when they touch here.
local GamePassId = 0 --Put the gamepassid here.

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local Character = Seat.Occupant and Seat.Occupant.Parent
    local Humanoid = Seat.Occupant
    local Player = Character and Players:GetPlayerFromCharacter(Character)

    if Player and Humanoid and not MarketplaceService:UserOwnsGamePassAsync(Player.userId, GamePassId) then
        Humanoid.Health = 0 --OR use "Seat.Disabled = true" to eject them.
    end
end)
4 Likes

Ah, the problem is the Seat. We all thought you meant a regular Part.
Pretty sure you need to use occupants to determine if the Player is sitting on it or not.

Yeah, I think so too.

(ignore this, this is for the character limit.)

It doesn’t seem to kill the player, I kept it at 0 for the gamepass ID so I made sure I didn’t have it in order to test and it didn’t kill me.

You need to set a gamepass id or it will error.

Ok it works now, thanks for your help. :slight_smile:

Please mark whichever post worked as the solution. :slight_smile:

Will do.
adding this for the 30 character limit