Hello! So I have a flashlight in my game and I have a flashlight battery in it. I have it so if you find batteries your flashlight battery goes up though its not working? I also want it so the player who found the battery gets the flashlight battery
LocalScript - Flashlight Script
local UIS = game:GetService("UserInputService")
local BadgeService = game:GetService("BadgeService")
local Debouce = false
local Player = game.Players.LocalPlayer
game.ReplicatedStorage.BatteryRecharge.OnClientEvent:Connect(function(player)
script.Parent.FlashlightScript.FlashlightBattery.Value = 100
end)
while wait(0.1) do
local BatteryGui = game.Players.LocalPlayer.PlayerGui.BatteryGui
if script.FlashlightBattery.Value < 25 then
BatteryGui.BatteryStatus.Image = "rbxassetid://11444586302"
elseif script.FlashlightBattery.Value < 50 then
BatteryGui.BatteryStatus.Image = "rbxassetid://11444607186"
elseif script.FlashlightBattery.Value < 75 then
BatteryGui.BatteryStatus.Image = "rbxassetid://11444611852"
elseif script.FlashlightBattery.Value < 100 then
BatteryGui.BatteryStatus.Image = "rbxassetid://11444616312"
elseif script.FlashlightBattery.Value < 0 then
BatteryGui.BatteryStatus.Image = ""
end
end
script.Parent.Equipped:Connect(function(player)
script.Parent.Front:WaitForChild("SurfaceLight").Enabled = true
script.Parent.Light:WaitForChild("Light").Enabled = true
script.Parent.Light:WaitForChild("Shadow").Enabled = true
if script.Parent.FlashlightScript.FlashlightBattery.Value == 0 then
game:GetService("BadgeService"):AwardBadge(player.UserId, 2129270521)
end
end)
script.Parent.Unequipped:Connect(function()
script.Parent.Front:WaitForChild("SurfaceLight").Enabled = false
script.Parent.Light:WaitForChild("Light").Enabled = false
script.Parent.Light:WaitForChild("Shadow").Enabled = false
end)
while wait(1) do
if script.Parent.Front.SurfaceLight.Enabled == true then
if script.Parent.FlashlightScript.FlashlightBattery.Value > 0 then
script.Parent.FlashlightScript.FlashlightBattery.Value -= 1
else
script.Parent.Front:WaitForChild("SurfaceLight").Enabled = false
script.Parent.Light:WaitForChild("Light").Enabled = false
script.Parent.Light:WaitForChild("Shadow").Enabled = false
end
end
end
Object Value (Stores the Flashlight Instance, which you can put under a folder, that isn’t named leaderstats, that is under the Player)
Loop though all the battery Instances in the removing script, and detect proximity prompts from them, check that the Player doing the prompt is the Player they use (We can use this with CollectionService and tags)
Other notes
I highly recommend using CollectionService to loop through all battery instances, from a script in ServerScriptSerivce, for easier readability, management, and I think performance, this method works best with options 1 and 2.