Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by 4 years ago

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.

0
Thanks so much Wiggles1305 49 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

I Got A Model That Will Work. Put The Swords In Lighting. Hopefully This Will Work, https://web.roblox.com/library/3492375069/Weapon-Shop

0
im sorry im not gonna take a freemodel Wiggles1305 49 — 4y
0
Its Literally By Me And My Group, Normally I Wouldn't Too But The Shop Is Super Good ricelicker 52 — 4y
0
dude this website is called scripting helpers, if you cant help us with our script then why even answer rower86 35 — 4y

Answer this question