Script Cannot Find Part In Character Model

Hello! I’m trying to create a flashlight system, that can also be toggled.

Basically, I have a script that clones over a block into the character model in Workspace, and follow the camera to where the player looks. (below)

local cam = workspace.CurrentCamera
local rs = game:GetService("ReplicatedStorage")
local rn = game:GetService("RunService")
local fl = rs.Flashlight
local clone = fl:Clone()
clone.Parent = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(.1, Enum.EasingStyle.Sine)

rn.RenderStepped:Connect(function()
	if clone then
		clone.Position = cam.CFrame.Position
		ts:Create(clone, ti, {CFrame = CFrame.lookAt(clone.Position, mouse.Hit.Position)}):Play()
	end
end)

And to toggle it, I have another script in Workspace with a RemoteEvent that is fired after pressing a key. (below)

model = script.Parent
wait(0.5)
currentP = "Off"
script.Parent.OnOff.OnServerEvent:Connect( function(player, pattern)
	if script.Parent.Value.Value == 0 then
		game.Workspace.Click:Play()
		player.Character.Flashlight.SpotLight.Enabled = true
		script.Parent.Value.Value = 1
	elseif script.Parent.Value.Value == 1 then
		game.Workspace.Click:Play()
		player.Character.Flashlight.SpotLight.Enabled = false
		script.Parent.Value.Value = 0
	end
end)

(basically how the code works above is pressing a key fires a remote event, which then causes this code to run and change values of a numbervalue)

However, the part “player.Character.Flashlight” does not work despite having the “Flashlight” part inside the Character model. What am I doing wrong? (Workspace image below, with error)


Error:

Seriously I have no idea what I’m doing wrong and I could use some help here. Please help me out!

The reason is due to the flashlight not loading yet

Are you sure? I added a “:WaitForChild” but it did not work

Try this code

model = script.Parent
wait(0.5)
currentP = "Off"
script.Parent.OnOff.OnServerEvent:Connect( function(player, pattern)
   local Flashlight=player.Character:WaitForChild("Flashlight")
	if script.Parent.Value.Value == 0 then
		game.Workspace.Click:Play()
		Flashlight.SpotLight.Enabled = true
		script.Parent.Value.Value = 1
	elseif script.Parent.Value.Value == 1 then
		game.Workspace.Click:Play()
		Flashlight.SpotLight.Enabled = false
		script.Parent.Value.Value = 0
	end
end)

Comes with a different error, (below)

is the flashlight welded?if it is then try anchoring

It’s unwelded and anchored. Not sure why the script can’t find it.

It doesn;t work because its only cloned in client.

It doesn’t exsist on server

Delete that in your local script

Insted create new server script using my server script:

local rs = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local fl = rs.Flashlight

Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local Clone = fl:Clone()
		Clone.Parent = char
	end)
end)

Yeah your right its only client side

1 Like

At first its hard to notice because he named some strange names like rs,rn,fl

yeah the variable definitions through me off

1 Like

Where would I put this? Sorry for that

1 Like

put inside of ServerScriptService

how is the localscript supposed to know what the variable for clone is supposed to be?

1 Like

If you are trying to get that variable in local script you can do

Note this is local script

local Player = game:GetService("Players").LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()

local FlashLight = Char:WaitForChild("Flashlight")

local cam = workspace.CurrentCamera
local rs = game:GetService("ReplicatedStorage")
local rn = game:GetService("RunService")

local mouse = game.Players.LocalPlayer:GetMouse()
local ts = game:GetService("TweenService")
local ti = TweenInfo.new(.1, Enum.EasingStyle.Sine)

rn.RenderStepped:Connect(function()
	if FlashLight then
		FlashLight.Position = cam.CFrame.Position
		ts:Create(FlashLight, ti, {CFrame = CFrame.lookAt(FlashLight.Position, mouse.Hit.Position)}):Play()
	end
end)

Thanks so much! I’ll mark you as the solution!

1 Like

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