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)
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.
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)
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)
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
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.