Hello there! I am currently trying to create a script that only allows certain members of a group and of certain role to acquire items from a proximity prompt designed to give them an item.
The problem I run into is when more than 1 person in-game, it gives the player who uses the prompt more than 1 of the designated items (if there are 2 players in-game the prompt will give the player 2 items).
This is my current code:
local groupid = 5038402
local ToolName = {"Sword"}
local Storage = game.ServerStorage
local Part = script.Parent.Parent
local ProximityPrompt = script.Parent
game.Players.PlayerAdded:Connect(function(player)
if player:GetRankInGroup(groupid) >= 3 then
ProximityPrompt.Triggered:connect(function(Player)
if Player and Player.Character then
local Backpack = Player:WaitForChild("Backpack")
for i = 1, #ToolName do
local Tool = Storage:FindFirstChild(ToolName[i])
if Tool then
Tool:clone().Parent = Backpack
end
end
end
end)
end
end)
That’s because your code runs twice, and therefore has two listeners for the .Triggered event.
I’d also like to revise your code, as a few lines could be altered or removed entirely.
local groupid = 5038402
local ToolName = {"Sword"}
local Storage = game.ServerStorage
local Part = script.Parent.Parent
local ProximityPrompt = script.Parent
ProximityPrompt.Triggered:connect(function(Player)
--Player and Player.Character will never be nil in this situation
if Player:GetRankInGroup(groupid) >= 3 then
local Backpack = Player.Backpack; --You also usually won't ever need to use :WaitForChild() on the server
for _, name in next, ToolName do --This loops thru ever entry in the ToolName table, and assigns 'name' to the entry
local Tool = Storage:FindFirstChild(name)
if Tool then
Tool:Clone().Parent = Backpack
end
end
end
end)
Remove player added and put the GetRankInGroup below the proximity:
local groupid = 5038402
local ToolName = {"Sword"}
local Storage = game.ServerStorage
local Part = script.Parent.Parent
local ProximityPrompt = script.Parent
ProximityPrompt.Triggered:connect(function(Player)
if Player:GetRankInGroup(groupid) >= 3 then
if Player and Player.Character then
local Backpack = Player:WaitForChild("Backpack")
for i = 1, #ToolName do
local Tool = Storage:FindFirstChild(ToolName[i])
if Tool then
Tool:clone().Parent = Backpack
end
end
end
end)
end