Can't get player string value properly

im making a gun loadout system and im almost done but im having issues on getting gun StringValue from the player and giving the player the weapon. heres the code

local ReplicatedStorage = game.ReplicatedStorage
local PrimaryFolder = ReplicatedStorage.Weapons.Primary

function module.Test(player)
	local primaryweapon = PrimaryFolder:FirstFindChild(player.EquippedPrimary.Value)
	primaryweapon.Parent = player.Character
	print(primaryweapon)
end

return module

Is there any error when running this?

yes

ServerScriptService.OnArrival.ModuleScript:7: attempt to index nil with ‘Value’

It’s likely something to do with how you’re giving it the player or how you’re trying to get the Value, is this function being ran immediately when the client joins?

How is EquippedPrimary created?

yea heres the code

local module = require(script.ModuleScript)
game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local players = {}
		for i, v in pairs(game.Players:GetPlayers()) do
			table.insert(players,v)
		end 
		module.Test(players)
	end)
end)

	local s= Instance.new("StringValue")
	s.Name = "EquippedPrimary"
	s.Parent = player

That’s the issue then, you’re not giving it time for EquippedPrimary to exist. Try replacing your .Test function with this

function module.Test(player)
	local equipped = player:WaitForChild("EquippedPrimary")
	local primaryweapon = PrimaryFolder:FirstFindChild(equipped.Value)
	primaryweapon.Parent = player.Character
	print(primaryweapon)
end

Wait wait, in this function it works if you give it a signle player, but you’re giving it a table of players?

But where? When? In a Script? It won’t work if you create or modify it in a LocalScript.

yea also the one you made still return the same error.

The function you made works if you give it a single palyer, but you’re giving it a table of players, that’s why it’s erroring, the function was not made to handle a table of players

its in a script not localscript

how do I make it for all players then?

function module.Test(players)
	for _,player in pairs(players) do
		local equipped = player:WaitForChild("EquippedPrimary")
		local primaryweapon = PrimaryFolder:FirstFindChild(equipped.Value)
		primaryweapon.Parent = player.Character
		print(primaryweapon)
	end
end

Or you can keep it the same and instead of returning a list of players, just put it in that in pairs loop you have and give it the player

theres a new error with it FirstFindChild is not a valid member of Folder “ReplicatedStorage.Weapons.Primary”

Oh right, it’s FindFirstChild, not FirstFindChild, you accidentally swapped the first two wrods around

1 Like

Do you actually define module at the top of the script?

local module = {}

local ReplicatedStorage = game.ReplicatedStorage
local PrimaryFolder = ReplicatedStorage.Weapons.Primary

function module.Test(player)
	local primaryweapon = PrimaryFolder:FirstFindChild(player.EquippedPrimary.Value)
	primaryweapon.Parent = player.Character
	print(primaryweapon)
end

return module

Also when you do module.Test(players), it uses a set of players and not just one player.

1 Like

Yea I defined the top of the script

it works thank you I had alot of struggles with it

1 Like

Anytime! IF you have anymore issues don’t be afraid to make another post!

1 Like