I’ve had this constant problem i’ve been trying to fix alone for a few days now. I managed to fix some of the code but the script still has an error that isn’t showing in the output.
its suppose to just give them ONE tool, but gives them 3 - 8. I noticed that it gives them as many tools as players that have joined or in-game. i.e : if theres 8 players in the game, you click it once and get 8 tools.
I need some feedback and help on how to fix this, the tool isn’t designed for more than 1 for each person to have, so having more breaks the game.
local ServerStorage = game.ReplicatedStorage
local tool = ServerStorage.Folder["Broad Sword"]
local owened = false
game.ReplicatedStorage.ItemHandler.OnServerEvent:Connect(function(player)
if owened == false then
owened = true
local toolcopy = tool:Clone()
toolcopy.Parent = player.Backpack
if player.Backpack:FindFirstChild("Broad Sword") then
owened = true
end
end
end)
Local Script:
local Event = game.ReplicatedStorage.ItemHandler
script.Parent.MouseButton1Click:Connect(function()
Event:FireServer()
end)
local ServerStorage = game.ReplicatedStorage
local tool = ServerStorage.Folder["Broad Sword"]
local owened = false
game.ReplicatedStorage.ItemHandler.OnServerEvent:Connect(function(player)
if owened == false then
local toolcopy = tool:Clone()
toolcopy.Parent = player.Backpack
if player.Backpack:FindFirstChild(toolcopy.Name) then
owened = true
else
print("Failed to parent tool to player")
end
else
print("Player already owns item")
end
end)
local Event = game.ReplicatedStorage.ItemHandler
local db = false
script.Parent.MouseButton1Click:Connect(function()
if not db then
db = true
Event:FireServer()
task.wait(5)
db = false
end
end)
I added a debounce and some better debugging, so you can figure out what is going on, let me know if something doesn’t work, also should be in Help and Feedback > Scripting Support
First of all, I recommend securing your remote events. I do not get why you are making owned as true twice. Nothing looks apparently wrong, so you should add a server sided debounce, per player.
local ServerStorage = game.ReplicatedStorage
local tool = ServerStorage.Folder["Broad Sword"]
local owened = false
game.ReplicatedStorage.ItemHandler.OnServerEvent:Connect(function(player)
if owened == false then
owened = true
local toolcopy = tool:Clone()
wait(.3)
tool:Clone()
toolcopy.Parent = ServerStorage.Folder
if not player.Backpack:FindFirstChild(toolcopy.Name) then
print(player)
tool.Parent = player.Backpack
elseif player.Backpack:FindFirstChild(toolcopy.Name) then
owened = true
end
end
end)
That slightly worked, it doesn’t give 1000 tools now, but now once one person gets the tool, no one else can get it. Theres no error when this happens
I’m glad that i helped at least a bit. That may happen because probably “owened” variable is checking but :FindFirstChild is alerady checking and since thats an serverscript owened never gets updated for each player so the fix is remove owened variable and everything that interacts with that variable in the script.
local ServerStorage = game.ReplicatedStorage
local tool = ServerStorage.Folder["Broad Sword"]
local owened = false
game.ReplicatedStorage.ItemHandler.OnServerEvent:Connect(function(player)
--if owened == false then
--owened = true
local toolcopy = tool:Clone()
if not player.Backpack:FindFirstChild(toolcopy.Name) then
print(player)
tool:Clone().Parent = player.Backpack
elseif player.Backpack:FindFirstChild(toolcopy.Name) then
--owened = true
end
end)
This script ended up fixing the errors, thanks for the help