Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My sword shop gui gives everyone items when one item is bought. Whats Wrong?

Whenever a player goes to buy a sword. The shop fires a value of what sword and what player it is and sends it to a server script that then is supposed to give them a sword. Instead it gives it to every player online.

Local Script

local SwordGive = game:GetService("ReplicatedStorage"):WaitForChild("RE"):WaitForChild("SwordGive")

local ReqGold = 10 -- Purchase Amount Here
local AvGold = game.Players.LocalPlayer.leaderboard.Gold --This is the player leaderboard stat for gold
local Rs = game.ReplicatedStorage.swords

script.Parent.MouseButton1Click:Connect(function()
	if AvGold.Value >= ReqGold then
		AvGold.Value = math.abs(AvGold.Value - ReqGold)
		SwordGive:FireServer("Poop")
	end
end)

Server Script

local SwordGive = game:GetService("ReplicatedStorage"):WaitForChild("RE"):WaitForChild("SwordGive")
local Swords = game:GetService("ReplicatedStorage"):WaitForChild("swords")

game.Players.PlayerAdded:Connect(function(player)
	SwordGive.OnServerEvent:Connect(function(plr, x)
		local backpack = game.Players[player.Name].Backpack
		print(player.Name)
		print(game.Players)
		if x == "Poop" then
			local sword = Swords:WaitForChild("PoopSword")
			local swordcopy = sword:Clone()
			swordcopy.Parent = backpack
		elseif x == "Stone" then
			local sword = Swords:WaitForChild("StoneSword")
			local swordcopy = sword:Clone()
			swordcopy.Parent = backpack
		elseif x == "Iron" then
			local sword = Swords:WaitForChild("IronSword")
			local swordcopy = sword:Clone()
			swordcopy.Parent = backpack
		elseif x == "Gold" then
			local sword = Swords:WaitForChild("GoldSword")
			local swordcopy = sword:Clone()
			swordcopy.Parent = backpack
		elseif x == "CastIron" then
			local sword = Swords:WaitForChild("CastIronSword")
			local swordcopy = sword:Clone()
			swordcopy.Parent = backpack
		elseif x == "LRed" then
			local sword = Swords:WaitForChild("LHealingPotion")
			local swordcopy = sword:Clone()
			swordcopy.Parent = backpack
		elseif x == "Speed" then
			local sword = Swords:WaitForChild("SpeedPotion")
			local swordcopy = sword:Clone()
			swordcopy.Parent = backpack
		end
	end)
end)

2 answers

✓ Accepted answer

The problem is in your server script. So your using an event when a player joins, which when the sword is actually purchased, it gives it to everyone in the game because your using the player variable from the join event. It's really hard to explain, let me fix your code lol.

local SwordGive = game:GetService("ReplicatedStorage"):WaitForChild("RE"):WaitForChild("SwordGive")
local Swords = game:GetService("ReplicatedStorage"):WaitForChild("swords")

SwordGive.OnServerEvent:Connect(function(player, x)
    local backpack = game.Players[player.Name].Backpack
    print(player.Name)
    print(game.Players)
    if x == "Poop" then
        local sword = Swords:WaitForChild("PoopSword")
        local swordcopy = sword:Clone()
        swordcopy.Parent = backpack
    elseif x == "Stone" then
        local sword = Swords:WaitForChild("StoneSword")
        local swordcopy = sword:Clone()
        swordcopy.Parent = backpack
    elseif x == "Iron" then
        local sword = Swords:WaitForChild("IronSword")
        local swordcopy = sword:Clone()
         swordcopy.Parent = backpack
    elseif x == "Gold" then
        local sword = Swords:WaitForChild("GoldSword")
        local swordcopy = sword:Clone()
        swordcopy.Parent = backpack
    elseif x == "CastIron" then
        local sword = Swords:WaitForChild("CastIronSword")
        local swordcopy = sword:Clone()
        swordcopy.Parent = backpack
    elseif x == "LRed" then
        local sword = Swords:WaitForChild("LHealingPotion")
        local swordcopy = sword:Clone()
        swordcopy.Parent = backpack
    elseif x == "Speed" then
        local sword = Swords:WaitForChild("SpeedPotion")
        local swordcopy = sword:Clone()
        swordcopy.Parent = backpack
    end
end)

If this helped, please upvote.

1 comment

0

Thanks so much

This discussion is preserved as it was. New answers, comments, and votes are closed.