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