Player able to buy more weapons when not wanted

Hello, the reason i made this topic because im having a problem with my Shop script(it works fine its just when the player clicks the gui it fires a remotevent sending it to server side where the player would get the weapon but i only want the player to get one of each weapon but the player is still able to get a bunch when your inside the lobby you can choose which weapon you want to use(only bat for now) but if you continue to click on it more bats would show up in your backpack when the match starts script: local OnWeapon = game.ReplicatedStorage.Events.OnWeapon
local hasWeapon = game.ReplicatedStorage.Values.HasWeapon
local Weapons = {
[“Bat”] = game.ReplicatedStorage.Weapons.Bat,
– add more weapons here
}

OnWeapon.OnServerEvent:Connect(function(player, char, hum)

local weaponName = player.Character.Weapon.Value
if player.Character.HumanoidRootPart:FindFirstChild("Weapons[weaponName]") then
	hasWeapon.Value = true
else if not player.Character.HumanoidRootPart:FindFirstChild("Weapons[weaponName]") then
		hasWeapon.Value = false
	end
	
end
if Weapons[weaponName] and hasWeapon.Value == false then
	local NewWeapon = Weapons[weaponName]:Clone()
	NewWeapon.Parent = player.Character.HumanoidRootPart
	hasWeapon.Value = true
	while wait(0.1) do
		if player.Character.InMatch.Value == true then
			NewWeapon.Parent = player.Backpack
				break
		end
	end
else
	print("can not get another weapon")
	-- the player's weapon does not match any weapon in the Weapons table
	
end

end)

2 Likes

the video is an mp3 file not an mp4?

1 Like

yea uh ima just remove the video

1 Like

the problem is if you were to click the bat gui from the shop it will show up in the hp(humanoidRootPart) but when you click it multiple times a bunch will show which i only want one of that weapon.

the fired remotevent is in a different script.

I think the issue is on these line of code:

if player.Character.HumanoidRootPart:FindFirstChild("Weapons[weaponName]") then
	hasWeapon.Value = true
else if not player.Character.HumanoidRootPart:FindFirstChild("Weapons[weaponName]") then
		hasWeapon.Value = false
	end
end

Because you’re FindFirstChild(“Weapons[weaponName]”) so the script will find object that have a name “Weapons[weaponName]” to fix this you should use:

if player.Character.HumanoidRootPart:FindFirstChild(Weapons[weaponName]) then
	hasWeapon.Value = true
elseif not player.Character.HumanoidRootPart:FindFirstChild(Weapons[weaponName]) then
		hasWeapon.Value = false
	end
end

also elseif statement should be call as elseif not else if.

Well, when I remove the quotes, I get another error I think the problem is the while wait loop.

1 Like

What’s the error say in ourput?

03:59:49.935 Argument 1 missing or nil - Server - GivePlayerWeapon:13 when I remove the quotes this occurs, I tried remaking that whole section, but this is all I got.

Maybe your weaponName variable just got nil value did you set value of your player.Character.Weapon.Value in local script? if yes then you need to set the value with server script or else server script will see it be nil

Yes, its server sided and its a stringvalue.

There might be another way to do this…

So yea its because of the elseif statement.

Okay can you try this?:

if player.Character.HumanoidRootPart:FindFirstChild(Weapons[weaponName].Name) then
	hasWeapon.Value = true
elseif not player.Character.HumanoidRootPart:FindFirstChild(Weapons[weaponName].Name) then
	hasWeapon.Value = false
end

also can you tell me what’s the value of weaponName?

Yea the problem is the elseif statement im trying to find other ways to get around it.

The Weapon Name is Called Bat.

Okay so that code should be work

finally, I figured it out😒 so instead of using the if and elseif statement i did this local OnWeapon = game.ReplicatedStorage.Events.OnWeapon
local hasWeapon = game.ReplicatedStorage.Values.HasWeapon
local Weapons = {
[“Bat”] = game.ReplicatedStorage.Weapons.Bat,
– add more weapons here
}

OnWeapon.OnServerEvent:Connect(function(player, char, hum)

local weaponName = player.Character.Weapon.Value
--if player.Character.HumanoidRootPart:FindFirstChild(Weapons[weaponName]) then
--	hasWeapon.Value = true

– elseif not player.Character.HumanoidRootPart:FindFirstChild(Weapons[weaponName]) then
– hasWeapon.Value = false
–end
print(Weapons[weaponName])
if Weapons[weaponName] and not player.Character.HumanoidRootPart:FindFirstChild(weaponName) then
print(hasWeapon.Value)
local NewWeapon = Weapons[weaponName]:Clone()
NewWeapon.Parent = player.Character.HumanoidRootPart
hasWeapon.Value = true
while wait(0.1) do
if player.Character.InMatch.Value == true then
NewWeapon.Parent = player.Backpack
break
end
end
–elseif not player.Character.HumanoidRootPart:FindFirstChild(“Weapons[weaponName]”) then
–hasWeapon.Value = false
–print(Weapons[weaponName])
– the player’s weapon does not match any weapon in the Weapons table
end
end)
which caused it to work! thank you for your help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.