So I have a function that parents a Beam to the Character of the player. That works.
However, I’m also trying to create another function where if the character touches a specific part, the Beam is deleted from the Character so that it’s no longer visible and can no longer be used but it doesn’t seem to work.
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local character = player.Character
part = script.Parent
function onTouch(hit)
if hit.Parent == character then
character:FindFirstChild("Beam"):Destroy()
end
end
part.Touched:connect(onTouch)
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local character = player.Character
part = script.Parent
function onTouch(hit)
if hit.Parent == character then
character:FindFirstChild("Beam", true):Destroy()
end
end
part.Touched:connect(onTouch)
You meant this, right? It’s not working.
The console’s not ouputting errors either. I tried searching for hints as to what can be wrong by adding print statements but none of them are even printing.
The part in question is a Union by the way.
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local character = player.Character
part = script.Parent
function onTouch(hit)
print("touched")
if hit.Parent == character then
print("character check success")
character:FindFirstChild("Beam", true):Destroy()
print("beam is destroyed")
else
print("whatever hit the part isnt a character.")
end
end
part.Touched:connect(onTouch)
If this is a LocalScript, its code will only run in specific containers in the game (e.g. when a descendent of the Backpack, PlayerScripts, PlayerGui, Character model).
This means that you would either need to move that LocalScript into one of the containers where its code can run, or, you can place the code into a Script that has its RunContext property set to Client, which would allow it to remain in the part.
Regardless of which solution you go with, I’d also recommend making some adjustments to the code to ensure that it works properly after respawning (since the variable for player.Character at the top of the script will currently only refer to the player’s original Character model and not the updated one when it respawns.
Example Revision
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local part = -- Reference to where the part is in the game
local function onTouch(hit)
local model = hit:FindFirstAncestorOfClass("Model")
if not model then return end
local Character = player.Character or player.CharacterAdded:Wait()
if Character and model == Character then
local Beam = Character:FindFirstChild("Beam")
--[[
If the Beam is not placed directly into the Character model, utilize the
suggestion mentioned in the previous reply of adding a second argument of
"true", without quotation marks, to the :FindFirstChild() call.
That would look through the descendants of the Character model
for an object called "Beam" if it doesn't find it on the first layer of
the Character model.
--]]
if Beam then
Beam:Destroy()
end
end
end
part.Touched:Connect(onTouch)
Lastly, take note that because this code is running on the client, the changes will only be visible to the specific player who touches the part. If other players are also no longer supposed to see the Beam from that Character model, then the code would need to be run on the server side so that the changes are visible to everyone in the server.
Yeah, the script was a LocalScript inside of the part to touch. The part was in Workspace.
What I did was change the LocalScript location from Workspace to StarterCharacterScripts and changed my code with the revised code, and of course ensuring that I defined the location of the part to touch.
Doing that resolved my issue. The beam disappears on touch. Thanks for the info on LocalScripts, I keep forgetting that they can be used in limited locations. Definitely won’t forget anymore.