Hello, so I am making a system that will give you a different key every time the event is fired so you don’t duplicate the keys and have the same key or the same key existing, and I am having some
difficulties with the script I wrote here.
local Key = game.ReplicatedStorage.Gears.Key
local ToolClone = game.ReplicatedStorage.Remotes.CloneTool
ToolClone.OnServerEvent:Connect(function(Player)
local RoomKeys = {101, 102, 103, 104, 105, 106, 107, 108, 109, 110}
local AlreadyRooms = {}
local RandomIndex = math.random(100, 110)
local Room_Key = Key:Clone()
if Player and Player:WaitForChild("Backpack"):FindFirstChild("Room Key "..tonumber(RandomIndex)) then
print("#1 | Server | Msg: test")
else
table.insert(AlreadyRooms, tonumber(RandomIndex))
Room_Key.Parent = Player:WaitForChild("Backpack")
Room_Key.Name = "Room Key (".. tonumber(RandomIndex) ..")"
Room_Key.Handle.Anchored = false
end
end)
One of the problems you can be running into is if the player has the tool equipped is it brought out of the backpack and into the character so it could be seen in the workspace.
There’s not really a necessity to be converting this into a number, cause in your RandomIndex variable:
There’s already gonna be a random number returned back by the math.random function
Anyways as other people have said, use tostring to convert variables into string values, and make sure that the Player either has the Tool in their Backpack or in their Character itself
Better yet, you could check if it’s in their StarterGear if you they have the tool permanently in the server but I’ll refer to the OP for now:
local Key = game.ReplicatedStorage.Gears.Key
local ToolClone = game.ReplicatedStorage.Remotes.CloneTool
ToolClone.OnServerEvent:Connect(function(Player)
local RoomKeys = {101, 102, 103, 104, 105, 106, 107, 108, 109, 110}
local AlreadyRooms = {}
local RandomIndex = math.random(100, 110)
local Room_Key = Key:Clone()
if Player then
if Player.Backpack:FindFirstChild("Room Key "..tostring(RandomIndex)) or Player.Character:FindFirstChild("Room Key "..tostring(RandomIndex)) then Player:WaitForChild("Backpack"):FindFirstChild("Room Key "..tonumber(RandomIndex)) then
print("#1 | Server | Msg: test")
else
table.insert(AlreadyRooms, RandomIndex)
Room_Key.Parent = Player:WaitForChild("Backpack")
Room_Key.Name = "Room Key (".. tostring(RandomIndex) ..")"
Room_Key.Handle.Anchored = false
end
end
end)
Instead of using tonumber use tostring and maybe try defining your table variables outside of the event. And try checking if the tool is in the Backpack or Character.
local Key = game.ReplicatedStorage.Gears.Key
local ToolClone = game.ReplicatedStorage.Remotes.CloneTool
local RoomKeys = {101, 102, 103, 104, 105, 106, 107, 108, 109, 110}
local AlreadyRooms = {}
ToolClone.OnServerEvent:Connect(function(Player)
if not Player then
return
end
local RandomIndex = math.random(101, 110)
local Room_Key = Key:Clone()
local Key_Name = "Room Key "..tostring(RandomIndex)
if Player.Backpack:FindFirstChild(Key_Name) or Player.Character
and Player.Character:FindFirstChild(Key_Name) then
print("#1 | Server | Msg: test")
else
table.insert(AlreadyRooms, RandomIndex)
Room_Key.Parent = Player.Backpack
Room_Key.Name = Key_Name
Room_Key.Handle.Anchored = false
end
end)