local rs = game:GetService("ReplicatedStorage")
local flashEvent = rs:WaitForChild("FlashEvent")
local players = game.Players.LocalPlayer
game.Players.PlayerAdded:Connect(function(player)
players = player
end)
local character = players.Character or players.CharacterAdded:Wait()
local ViewModelValue = game.StarterGui.VIEWMODEL.Values.ViewModelValue.ViewModel
local FlashlightViewModelValue = game.StarterGui.VIEWMODEL.Values.ViewModelValue.Flashlight
local flashlight = nil
flashEvent.OnServerEvent:Connect(function(player)
flashlight = player.Character.HumanoidRootPart:FindFirstChild("SurfaceLight")
if not flashlight then
ViewModelValue.Value = true
FlashlightViewModelValue.Value = true
local flashlight = Instance.new("SurfaceLight")
flashlight.Color = Color3.fromRGB(255, 255, 255)
flashlight.Brightness = 1
flashlight.Angle = 99
flashlight.Range = 50
flashlight.Parent = player.Character:WaitForChild("HumanoidRootPart")
else
ViewModelValue.Value = false
FlashlightViewModelValue.Value = false
flashlight:Destroy()
end
end)
game:GetService("RunService").RenderStepped:Connect(function()
if players and character and character:FindFirstChild("Humanoid") and character.Humanoid.Health <= 0 then
flashlight:Destroy()
end
end)
Here is my current script, I would like to destroy Flashlight if the player dies, Please help me
You don’t have to do that, bruh. As previously said, you can use Humanoid.Died in the server script directly. When the local player dies, the Humanoid.Died has almost the same role. Every time a player dies, Humanoid.Died detects it. If you want to check in a localscript, you’d have to utilize remotes, which is pointless.
I’d use a Humanoid.Died connection, also you shouldn’t be doing “local Players = game.Players.LocalPlayer” in a server-script as it wouldn’t work, this would only work in a LocalScript, here’s a better way to do it (Edit it as you will as I don’t think It would function in your game perfectly fine)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FlashEvent = ReplicatedStorage:WaitForChild("FlashEvent")
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
assert(HumanoidRootPart, "Couldn't find HumanoidRootPart")
local Humanoid = Character:FindFirstChild("Humanoid")
assert(Humanoid, "Couldn't find Humanoid")
Humanoid.Died:Connect(function()
if HumanoidRootPart:FindFirstChild("SurfaceLight") then
HumanoidRootPart.SurfaceLight:Destroy()
end
end)
end)
end)
local ViewModelValue = StarterGui:FindFirstChild("VIEWMODEL").Values.ViewModelValue.ViewModel
local FlashlightViewModelValue = StarterGui:FindFirstChild("VIEWMODEL").Values.ViewModelValue.Flashlight
FlashEvent.OnServerEvent:Connect(function(Player)
local HumanoidRootPart = Player.Character:FindFirstChild("HumanoidRootPart")
assert(HumanoidRootPart, "Couldn't find HumanoidRootPart")
local Flashlight = HumanoidRootPart:FindFirstChild("SurfaceLight")
if not Flashlight then
ViewModelValue.Value = true
FlashlightViewModelValue.Value = true
local SurfaceLight = Instance.new("SurfaceLight")
SurfaceLight.Color = Color3.fromRGB(255, 255, 255)
SurfaceLight.Brightness = 1
SurfaceLight.Angle = 99
SurfaceLight.Range = 50
SurfaceLight.Parent = HumanoidRootPart
else
ViewModelValue.Value = false
FlashlightViewModelValue.Value = false
Flashlight:Destroy()
end
end)