Player variable overwriting every-time a player joins

What I want is a script where when the player presses the ProximityPrompt it will trigger a remote event containing the player, the crate referenced and the location of the crate tool then it that data is used to: Fire an animation making the player hold the crate, that works perfectly. The part isn’t visible on server-side but I know I could address that easily, my issue is as follows:

Player 1 joins the game
Player 1 presses the proximity prompt to get a crate, it works and they have the animation in their hand as well as a client-side crate
Player 2 joins the game
Player 1 wants to collect a crate but when they press it player 2 has now overwriten the player variable meaning any requests for crates go directly to player 2 so whenever player 1 goes to collect a crate it ends up making it so player 2 plays the animation and gets the crate.
Player 3 joins the game and now any requests go to them meaning the animation will trigger for them and they will get a crate if player 1 or 2 decide to request a crate.

Script:

local players = game:GetService("Players")
local proxPrompt = script.Parent.ProximityPrompt
local playerv
local Crate = {"BeginnerBoat", "AnotherBoat", "YetAnotherBoat"}
local selectedCrate = "BeginnerBoat"
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("AssignCrate")
local finalCrate
local toConvert

local function onPlayerAdded(player)
	playerv = player
end

players.PlayerAdded:Connect(onPlayerAdded)

function AssignCrate(obj)
	for i, value in ipairs(Crate) do 
		if value == selectedCrate then 
			toConvert = value
			local finalCrate = (tostring(toConvert))
			print("Crate Selected:"..finalCrate)
			local selectedTool = game.Lighting.Inv.Crates:FindFirstChild(finalCrate)
			remoteEvent:FireClient(playerv, finalCrate, selectedTool)
			break
		end


	end

end
proxPrompt.Triggered:Connect(function()
	print("Triggered")
	AssignCrate()

end)

Local script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("AssignCrate")
local players = game:GetService("Players")
local player = game.Players.LocalPlayer
local char = player.Character
local humanoid = char:WaitForChild("Humanoid")
local Tool
local function Animation(AnimationID)
	print("Fire Animation")
	local animation = Instance.new("Animation")
	animation.AnimationId = "http://www.roblox.com/asset/?id="..AnimationID
	animation.Parent = char
	local animationTrack = humanoid:LoadAnimation(animation)
	animationTrack:Play()
	return animationTrack

end

local function giveCrate(finalCrate, selectedTool)
	Animation("6255322549")
	print("Crate: "..finalCrate)
	print(selectedTool)
	Tool = selectedTool:Clone()
	Tool.Parent = player.Backpack
	humanoid:EquipTool(Tool)
	print(player)
	
end








remoteEvent.OnClientEvent:Connect(giveCrate)

Because you have PlayerV as a variable at the top of your script, the current value of playerv will be used for all functions using that variable, instead create the playerv variable only inside of the function using it, that way it shouldnt override others

1 Like