Need help with Beams

can anyone tell me how to add a ‘beam’ object to an avatar ingame?

i have a script that works but the beam still does not show ingame, i need help
this is the script;

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

local ActivationPhrase = "aura on"
local DeactivationPhrase = "aura off"

local function WaitForCharacter(plr)
	local maxWaitTime = 10 -- Maximum time to wait for the character to load (in seconds)
	local startTime = tick()
	while tick() - startTime <= maxWaitTime do
		local Character = plr.Character
		if Character then
			return Character
		end
		wait(0.1) -- Wait for a short time before checking again
	end
	return nil
end

local function ActivateRing(plr)
	local Character = WaitForCharacter(plr)

	if Character then
		local RightArm = Character:FindFirstChild("Right Arm")
		if RightArm then
			local Ring = game.ReplicatedStorage.Assets.Ring:Clone()
			Ring.Name = "Ring"

			-- Create a weld between the Ring and the Right Arm
			local Weld = Instance.new("Weld")
			Weld.Parent = RightArm
			Weld.Part0 = RightArm
			Weld.Part1 = Ring
			Weld.C0 = RightArm.CFrame:inverse() * Ring.CFrame

			for _, v in pairs(Ring:GetDescendants()) do
				if v:IsA("Beam") then
					v.Enabled = true
				end
			end
			print("Ring Attached to Right Arm and Enabled")
		else
			print("ActivateRing Failed: Right Arm not found")
		end
	else
		print("ActivateRing Failed: Character not found")
	end
end

local function DeactivateRing(plr)
	local Character = WaitForCharacter(plr)

	if Character then
		local Ring = Character:FindFirstChild("Ring")
		if Ring then
			Ring:Destroy()
			print("Ring located and destroyed")
		else
			print("Ring not found in character")
		end
	else
		print("DeactivateRing Failed: Character not found")
	end
end

Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local LowerMessage = msg:lower()
		if LowerMessage == ActivationPhrase then
			ActivateRing(plr)
		elseif LowerMessage == DeactivationPhrase then
			DeactivateRing(plr)
		end
	end)
end)

When you test the game in Studio, select yourself in the Explorer window (not in the Players folder) to see if the ring is there, and if the Beam is in the ring.

Also, why are you using a WaitForCharacter function? Why not use WaitForChild?

In your ActivateRing function you could first do

If not Character then 
   --send them to your WaitForCharacter function instead of sending the script there when it doesn't need to
else
    --means that if Character == true your code runs
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.