Weapon Loudout GUI not working

So i am currently making a weapon selection menu that using image buttons that once clicked, trigger and event that changes the value “SelectedGun” to the selected weapon. However, for some reason the server script doesnt work.
local:

local player = game.Players.LocalPlayer
local char = game.Workspace:FindFirstChild(player.Name)

script.Parent.MouseButton1Click:Connect(function()
	if char:FindFirstChild("SelectedGun").Value == "Kar98" or "M1 garand" or "P38" then
		script.Parent.RemoteEvent:FireServer(player)
	end
end)```

Server:

```lua
local RemoteEvent = script.Parent.RemoteEvent

RemoteEvent.OnServerEvent:Connect(function(char)
	if char then
		char:FindFirstChild("SelectedGun").Value = game.ReplicatedStorage:FindFirstChild("Weapons"):FindFirstChild("Kar98k")
	end
end)```
if char:FindFirstChild("SelectedGun").Value == "Kar98" or "M1 garand" or "P38" then
-- you can't do this
local player = game.Players.LocalPlayer
local char = game.Workspace:FindFirstChild(player.Name)

script.Parent.MouseButton1Click:Connect(function()
	local SelectedGun = char:FindFirstChild("SelectedGun")

	if SelectedGun then
		local SelectedName = SelectedGun.Value.Name

		if SelectedName == "Kar98" or SelectedName == "M1 garand" or SelectedName== "P38" then
			script.Parent.RemoteEvent:FireServer(player)
		end
	end
end)
1 Like

what i did works just fine, the value is an ObjectValue. Also, the issue is in the server script

  1. it’s works fine because condition always returns true
  2. can u show what server outputs

Players.MeloettatheAwsomeOne.PlayerGui.MarleyLoadout.Frame.Guns.ScrollingFrame.Kar98.Script:5: attempt to index nil with ‘Value’

yeah it’s because the condition return true even if there are nil in selectedGun.

also i recommend to you to change server script to this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = script.Parent.RemoteEvent

RemoteEvent.OnServerEvent:Connect(function(char)
	if char then
		local SelectedGun = char:FindFirstChild("SelectedGun"); if not SelectedGun then return end
		local Weapons = ReplicatedStorage:FindFirstChild("Weapons"); if not Weapons then return end
		local Kar98k = Weapons:FindFirstChild("Kar98k"); if not Kar98k then return end
		
		SelectedGun.Value = Kar98k
	end
end)

no errors but still doesnt work

Have you changed both scripts?

Never mind, i got it to work I just changed a few things

instead of checking for each weapon, you could have an array of weapon names

-- LocalScript
local RS = game:GetService("ReplicatedStorage")
local WeaponSelector: RemoteEvent = RS.WeaponSelector -- Better name for your remote event

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Selection = script.Parent
local Weapons = {"Kar98", "M1 garand", "P38"} -- Array of weapons instead of checking each weapon

Selection.MouseButton1Click:Connect(function()
	local SelectedGun = Character:FindFirstChild("SelectedGun")
	if SelectedGun and table.find(Weapons, SelectedGun) then -- Search for selected weapon in weapons array
		RS.WeaponSelector:FireServer(--[[You don't need to pass LocalPlayer through remotes]])
	else
		print("Player has no gun selected") -- Or the weapon isn't a 'real' weapon (not in weapons array)
	end
end)
-- ServerScript
local RS = game:GetService("ReplicatedStorage")
local WeaponSelector: RemoteEvent = RS.WeaponSelector -- Better name for your remote event

WeaponSelector.OnServerEvent:Connect(function(Player --[[ OnServerEvent returns the player that fired, not the player's character]])
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local SelectedGun = Character:FindFirstChild("SelectedGun")
	if SelectedGun then
		SelectedGun.Value = RS.Weapons:FindFirstChild(SelectedGun.Value)
	end
end)

i’d suggest using a RemoteFunction instead

-- ServerScript
RemoteEvent.OnServerEvent:Connect(function(Player, ...)
  -- Player (The Player/Client who fired the RemoteEvent) will always be the first argument for OnServerEvent
  local Character = Player.Character or Player.CharacterAdded:Wait()
  print(Character.Name, ...)
end)
-- LocalScript
local Inventory = {["Apples"] = 3}
RemoteEvent:FireServer(Inventory)
-- Output --> BonesIsUseless, {["Apples"] = 3}