Script Issues ._. Player detection, etc

Alrighty the ultimate point of my script is if a player (NOT A HUMANOID) is touching a “bad part”, an event fires. (And it will have a 10 second cooldown so you dont have to worry about detecting when they leave the part, or spam touching it multiple times, or anything like that). However if they are touching a “good part” at the same time they are touching a “bad part”, nothing happens.

Okay here’s my pathetic script so far but this doesn’t work and even if it did I don’t know where to go from there.

local part = script.Parent

local function detect(otherPart)
	local partParent = otherPart.Parent
	local player = game.Players:GetPlayerFromCharacter(partParent)
	if player then
		print("Player Touched")
	end
end

okay bye

2 Likes

In the script you have provided you haven’t ran the function.

local part = script.Parent

local function detect(otherPart)
	local partParent = otherPart.Parent
	local player = game.Players:GetPlayerFromCharacter(partParent)
	if player then
		print("Player Touched")
	end
end

part.Touched:Connect(detect)
1 Like

haha whoops

do you know where to go from here

1 Like

The way I would implement this is by having 1 script that controls both part’s .Touched event. You’re on the right track though.

Example code (may not work as intended):

--//Services
local Players = game:GetService("Players")

--//Variables
local Model = script.Parent
local goodPart = Model.GoodPart
local badPart = Model.BadPart

--//Tables
local playersTouchingGoodPart = {}
local playersTouchingBadPart = {}

--//Functions
goodPart.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	
	if not player or table.find(playersTouchingGoodPart, player) then
		return
	end
	
	table.insert(playersTouchingGoodPart, player)
	
	local playerFound = table.find(playersTouchingBadPart, player)
	
	if playerFound then
		table.remove(playersTouchingBadPart, playerFound)
	end
end)

goodPart.TouchEnded:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)

	if not player then
		return
	end

	local playerFound = table.find(playersTouchingGoodPart, player)

	if playerFound then
		table.remove(playersTouchingGoodPart, playerFound)
	end
end)

badPart.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)

	if not player or table.find(playersTouchingBadPart, player) or table.find(playersTouchingGoodPart, player) then
		return
	end

	table.insert(playersTouchingBadPart, player)
	
	while task.wait(0.1) and table.find(playersTouchingBadPart, player) do
		--//Bad part shenanigans
		player.Character.Humanoid.Health -= 1
	end
end)

badPart.TouchEnded:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)

	if not player then
		return
	end

	local playerFound = table.find(playersTouchingBadPart, player)

	if playerFound then
		table.remove(playersTouchingBadPart, playerFound)
	end
end)

I need more details though, because I’m not sure what you want to happen.

Yes. In my reply, I have added part.Touched:Connect(detect). What this does it when the part is touched, it will run the function ‘detect’ function.

The details are the bad part is like a laser that moves back and forth. It doesn’t kill you it just spawns an enemy, which is why at first I wanted cooldown, but I realized cooldown is kind of un-immersive and not really necessary with how slow the laser moves. So yeah on second thought maybe it should only fire the event when the player touches the laser for the first time but resets when the player leaves. Oh and the good part is a safe zone…

Okay I’ve tinkered with it a bit and good part does nothing so I’m not sure just having it remove players from the bad table is effective or fast enough…