How do I make this local script work every time the local player joins or respawns? I am very confused.
The script below is what I tried.
(I just realized that it supposed to be just game.Players but I’m trying to do it for the local player)
local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
wait(1)
player.CharacterAdded:Connect(function()
if player.aye.gun.Value == "gun3" then
ReplicatedStorage.Events.gunEvent5:FireServer()
end
end)
What you’re doing is kinda right, this script will run when the player respawns, but not joins.
Local scripts are created when the local player joins the game so they run immediately, the character was most likely created by that time, so just run the code both when joins and when respawns:
local player = game.Players.LocalPlayer
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local value = player:WaitForChild("aye"):WaitForChild("gun") -- Will wait until these exist
local function checkAndFire() --Putting that in a function will just shorten the code.
if value.Value == "gun3" then --Player just joined
ReplicatedStorage.Events.gunEvent5:FireServer() --Fire the event
end
end
checkAndFire() --Run when player joins
player.CharacterAdded:Connect(checkAndFire) --Run when character is created
I was just about to suggest OP makes a function so he doesn’t have to write the code twice when the script is made and when the player respawns, you beat me to it lmao