How do I make this script work everytime the player respawns?

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)

do you know how to make a loop?

No how would I do that, the loop?

Where’s the script located? This could be related to why it isn’t working because from what I see, it should work

The script is located in starterplayerscripts

Remove the wait. Also you really should be doing this on the server as this is very easily exploitable.

2 Likes

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
1 Like

What @LightningLion58 mentioned does seem to be the case even though @jonny377 didn’t mention which parts were working and which weren’t.

2 Likes

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

2 Likes

Thank You! I learned a lot from this script.