How do I make a part from a tool touch a part and trigger something

Hey there! So for my game I’m working on a system that uses a Lighter too to light candles around the map. I used this script to make it so when a part inside the Lighter tool touches the candle, it lights it.

Error: Workspace.Candle.Script:5: attempt to index nil with ‘Lighter’ - Server - Script:5

local candle = script.Parent.LightTrigger
local player = game.Players.LocalPlayer

candle.Touched:Connect(function(Part)
	if Part == player.Lighter.Lighter then
		script.Parent.Flame.CandleLight.Enabled = true
		script.Parent.Flame.Flame.Enabled = true
		print("HI")
	end
end)
6 Likes

This happens because player.Lighter.Lighter is most likely not what you’re looking for in this context. If you want a part inside of a tool to be touching another part, the tool itself needs to be inside the player’s character.

I would actually just recommend listening for when the lighter itself touches other parts, check if they’re candles, and then lighting the candle up.

1 Like

Oh okay, how do you recommend doing that? By the way I’m super rusty with scripting so I’ve forgotten some of the terms

You can use CollectionService to add Tags to the candle objects. I’ve made a reference for you.

local CollectionService = game:GetService("CollectionService")
local Tool = script.Parent

Tool.Handle.Touched:Connect(function(hit)
	if CollectionService:HasTag(hit, "Candle") then
		-- // Activate candle effects using "hit" to reference the candle that was touched
	end
end)

You can add Tags to objects through the Properties window while having the object selected.

image

1 Like

Alrighty, I’ll give this a try!

Okay here’s what I put. I get no error so I’m not sure what I did wrong.

local CollectionService = game:GetService("CollectionService")
local Tool = script.Parent

Tool.Lighter.Touched:Connect(function(hit)
	if CollectionService:HasTag(hit, "Candle") then
		hit.Parent.Flame.Flame.Enabled = true
		hit.Parent.Flame.CandleLight.Enabled = true
	end
end)

Did you add the “Candle” tag to the candle parts in workspace?

Yup, I did. Had to enable the beta feature

you cant get local player from server script, getting local player would on work in a local script if you only want it to show and run on one persons computer. You need to do this instead:

local candle = script.Parent.LightTrigger

game.Players.PlayerAdded:Connect(function(player)

candle.Touched:Connect(function(Part)
	if Part == player.Lighter.Lighter then
		script.Parent.Flame.CandleLight.Enabled = true
		script.Parent.Flame.Flame.Enabled = true
		print("HI")
	end
end)

end

That’s why player comes as nil, because server script cant get a player locally, only server side

I would not recommend doing this as this would create a new event every single time a player joins. It would also only work for that one candle you’re referencing. Very unoptimized solution that I still think wouldn’t work because of player.Lighter.Lighter.

I’ve tested the solution that I posted previously, and it works just fine.
GIF (gyazo.com)

The part that’s meant to be touched might be too small. Try creating a hitbox around the candle, that if touched, would active the effect on the smaller part.

so a client to server event better for this situation?

No, you can use a Script inside of the Tool itself for the .Touched function.

I have done that, when I mean “Lighter.Lighter” the second lighter is the hitbox. It’s pretty big around the whole lighter

I’ll try this method real quick

Getting there, but I get an error saying: Lighter is not a valid member of Player “Players.z71226”

Did you use the same script that I used for the GIF?

Yes, I just created a hitbox around the tool and candles, so they’re easily activated.