This previous post of mine is related to the issue I’m having; however, I will be going over the specific issue I’m having in this post!
My flashlight has an On/Off feature; however, the Part which has the light is in the Workspace. I’ve tried to add that into my script; however, it does not seem to work when trying to turn the flashlight on & off.
I also would like the game to be multiplayer and having the Part just spawn randomly in the Workspace is not very ideal since if there are, let’s say two players in the game, then two Parts will be within the Workspace which will then cause issues (I believe).
Flashlight
Workspace Handler script
game:GetService("RunService").RenderStepped:Connect(function()
local Player = game:GetService("Players")
local LPlayer = Player.LocalPlayer
local PlayerGUI = LPlayer:WaitForChild("PlayerGui")
local Flashlight = script.Parent
local GUI = PlayerGUI:FindFirstChild("FlashlightGUI")
local Settings = require(Flashlight.Settings)
if Settings.BatteryWaste == true then
GUI.Enabled = true
else
GUI.Enabled = false
end
GUI.Percent.Text = tostring(math.floor(Flashlight.Battery.Value + 0.5)).."%"
local Warning = GUI:FindFirstChild("Warning")
local Battery = Flashlight:WaitForChild("Battery")
Battery.Changed:Connect(function()
if Flashlight.Battery.Value <=20 then
game.Workspace.Sounds.Beep:Play()
Warning.Visible = true
else
Warning.Visible = false
end
end)
end)
Function script
local On = false
local Settings = require(script.Parent.Settings)
function Setup()
script.Parent.Handle.Light.SpotLight.Angle = Settings.Angle
script.Parent.Handle.Light.SpotLight.Angle = Settings.Range
script.Parent.Handle.Light.SpotLight.Angle = Settings.Color
script.Parent.Handle.Light.SpotLight.Angle = Settings.Brightness
end
Setup()
script.Parent.RemoteEvent.OnServerEvent:Connect(function()
if On == false then
On = true
script.Parent.Handle.SwitchOff.Transparency = 1
script.Parent.Handle.SwitchOn.Transparency = 0
script.Parent.Handle.Light.Material = Enum.Material.Neon
script.Parent.Handle.Light.SpotLight.Enabled = false --true
script.Parent.Handle.Light.SpotLight2.Enabled = false --true
script.Parent.Handle.Sound:Play()
script.Parent.Parent.Parent.Workspace.FlashPart.Spotlight.Enabled = true
while (On) do
wait()
if Settings.BatteryWaste == true then
wait()
script.Parent.Battery.Value = script.Parent.Battery.Value - Settings.BatteryWasteAmount
script.Parent.Battery.Value = (script.Parent.Battery.Value < 0 and 0 or script.Parent.Battery.Value)
end
end
else
On = false
script.Parent.Handle.SwitchOff.Transparency = 0
script.Parent.Handle.SwitchOn.Transparency = 1
script.Parent.Handle.Light.Material = Enum.Material.Glass
script.Parent.Handle.Light.SpotLight.Enabled = false
script.Parent.Handle.Light.SpotLight2.Enabled = false
script.Parent.Handle.Sound:Play()
script.Parent.Parent.Parent.Workspace.FlashPart.Spotlight.Enabled = false
end
end)
script.Parent.Unequipped:Connect(function()
On = false
script.Parent.Handle.SwitchOff.Transparency = 0
script.Parent.Handle.SwitchOn.Transparency = 1
script.Parent.Handle.Light.Material = Enum.Material.Glass
script.Parent.Handle.Light.SpotLight.Enabled = false
script.Parent.Handle.Light.SpotLight2.Enabled = false
script.Parent.Parent.Parent.Workspace.FlashPart.Spotlight.Enabled = false
end)
Remote script
local player = game.Players.LocalPlayer
script.Parent.Activated:Connect(function()
script.Parent.RemoteEvent:FireServer()
end)
script.Parent.Equipped:Connect(function()
local gui = script.Parent.FlashlightGUI:Clone()
gui.Parent = player.PlayerGui
script.Parent.Unequipped:Connect(function()
gui:Destroy()
end)
end)
Why the heck would you put the SpotLight Part in the workspace and not inside the flashlight itself?
I have a flashlight with the Spotlight in the flashlight Tool. I weld the flashlight to the player’s arm so that when they move during animations the flashlight and Spotlight swing with the arm.
It’s simple and effective.
Understandable; however, that’s the issue I was having originally — that the light would swing with the arm. I wanted the light to move with the player’s camera/mouse.
Is there a way I can just have the player’s arm move instead when holding the flashlight?
No, I was trying to connect the function in the StarterCharacterScript to the other function in the Flashlight tool. I guess that’s just not the way to go about it… sigh
Perhaps, I have a Settings Script that alters the amount of light produced by the flashlight from the original script. I kept it in cause if this didn’t work, I could just start over.
I’m currently reading that page and it’s very confusing — I need 3 scripts in ServerScriptService? One to create the function, one to receive the data, and one to send the data? Also, how am I going to connect it to the localscript within the StarterPlayerCharacter?
You could probably use remote events instead.
So, in one script, you can have the function start when the remote event is fired, like this:
game.ReplicatedStorage.insert_name_here.OnServer/ClientEvent:Connect(function()
-- do stuff here
end)
and in the other
-- insert code here
if blah then -- doesn't have to be an if statement just here for yk placeholder
game.ReplicatedStorage.insert_name_here.FireClient/Server
Sorry if I’m being confusing, there’s probably someone who could say it better
local RunService = game:GetService("RunService")
local Player = game.Players.LocalPlayer
local PlayerMouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local Character = Player.Character or Player.CharacterAdded:Wait()
local Head = Character:WaitForChild("Head")
local Neck = Head:WaitForChild("Neck")
local Torso = Character:WaitForChild("UpperTorso")
local Waist = Torso:WaitForChild("Waist")
local Arm = Character:WaitForChild("RightUpperArm")
local Shoulder = Arm:WaitForChild("RightShoulder")
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local NeckOriginC0 = Neck.C0
local ShoulderOriginC0 = Shoulder.C0
local WaistOriginC0 = Waist.C0
Neck.MaxVelocity = 1/2
RunService.RenderStepped:Connect(function()
local CameraCFrame = Camera.CoordinateFrame
if Character:FindFirstChild("UpperTorso") and Character:FindFirstChild("Head") then
local ArmLookVector = Arm.CFrame.lookVector
local HeadPosition = Head.CFrame.p
local TorsoLookVector = Torso.CFrame.lookVector
if Neck and Shoulder and Waist then
if Camera.CameraSubject:IsDescendantOf(Character) or Camera.CameraSubject:IsDescendantOf(Player) then
local Point = PlayerMouse.Hit.p
local Distance = (Head.CFrame.p - Point).magnitude
local Difference = Head.CFrame.Y - Point.Y
Neck.C0 = Neck.C0:lerp(NeckOriginC0 * CFrame.Angles(-(math.asin(Difference / Distance) ), (((HeadPosition - Point).Unit):Cross(ArmLookVector)).Y, 0), .5 / 2)
Shoulder.C0 = Shoulder.C0:lerp(ShoulderOriginC0 * CFrame.Angles(-(math.asin(Difference / Distance)), (((HeadPosition - Point).Unit):Cross(ArmLookVector)).Y, 0), .5 / 2)
Waist.C0 = Waist.C0:lerp(WaistOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.5, 0), 0.5 / 2)
end
end
end
end)