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

Buying a weapon causes all players to pay for it?

Asked by 3 years ago

i'm working on a call of duty: zombies remake and everything works fine in singleplayer but when in multi-player buying a weapon causes all players in game to buy it too - i want it so the person that buys the weapon is the only one who pays for it and also is the only one who receives it

local ProximityPrompt = script.Parent.PromptAttachment.ProximityPrompt
upgradecost = 1000

ProximityPrompt.Triggered:Connect(function(player)
        local p = game.Players:GetChildren()
        for i = 1, #p do                                  
            local cash = p[i].leaderstats:findFirstChild("Points")
            if (p[i].Name ~= script.Owner.Value) then
            if  cash.Value > upgradecost or cash.Value == upgradecost then
                script.Parent.Reload:Play()
                    game.Lighting:findFirstChild("Sawn-off"):clone().Parent = p[i].Backpack
                    cash.Value = cash.Value - upgradecost
                end 
        end
    end
end)

1 answer

Log in to vote
0
Answered by 3 years ago

Here are some errors I have found

Line 11: You only want to do it to the player that bought it, since it is a for loop doing P[i] does it for every single player. Line 8: I'm not sure if this is an error but why is it so that the code runs if the player name is not equal to it?

There are also some ways to do this better, I rewrote your code to use the player parameter passed in your proximity prompt.

ProximityPrompt.Triggered:Connect(function(player)

    local cash = player.leaderstats.Points

    if cash.Value >= upgradecost then

        -- do your buying stuff here

    end
end)

The code above is untested so I am unsure if it works.

The code above is better than a for loop for this situation but if you really want to here is the code for the for loop.

ProximityPrompt.Triggered:Connect(function(player)
    for i, child in pairs(game.Players:GetChildren) do
        if child == player then
        -- do your buying stuff here
        end
    end
 end)

Ad

Answer this question