Hello! I have an item box manager. Is it possible to make this code more reliable?
The Code
local items = {
-- This is the table that manages the items. It is as reliable as it can get.
}
local plrItems
game.Players.PlayerAdded:Connect(function(plr)
print("Started")
local map = game.Workspace:WaitForChild("Map")
print("Map")
local itemBoxes = map:WaitForChild("ItemBoxes")
print("ItemBoxes")
print("PlayerAdded")
plrItems = Instance.new("Folder")
plrItems.Parent = game.ReplicatedStorage:WaitForChild("PlayerItems")
plrItems.Name = plr.Name.."Items"
local item1 = Instance.new("StringValue")
item1.Parent = plrItems
item1.Name = "item1"
item1.Value = "None"
local uses = Instance.new("IntValue")
uses.Parent = item1
uses.Name = "Uses"
uses.Value = 0
local item2 = Instance.new("StringValue")
item2.Parent = plrItems
item2.Name = "item2"
item2.Value = "None"
local uses2 = Instance.new("IntValue")
uses2.Parent = item2
uses2.Name = "Uses"
uses2.Value = 0
local choosing = Instance.new("BoolValue")
choosing.Parent = plrItems
choosing.Name = "Choosing"
choosing.Value = false
local choosing2 = Instance.new("BoolValue")
choosing2.Parent = plrItems
choosing2.Name = "Choosing2"
choosing2.Value = false
local plrRemote = Instance.new("RemoteEvent")
plrRemote.Parent = game.ReplicatedStorage.PlayerRemotesFolder
plrRemote.Name = plr.Name.."AddItem"
local plrRemote = Instance.new("RemoteEvent", game.ReplicatedStorage.PlayerRemotesFolder)
plrRemote.Name = plr.Name.."SwitchItem"
end)
print("Started")
local map = game.Workspace:WaitForChild("Map")
print("Map")
local itemBoxes = map:WaitForChild("ItemBoxes")
print("ItemBoxes")
for i, ch in pairs(itemBoxes:GetDescendants()) do
if ch:IsA("BasePart") then
ch.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if ch.Debounce.Value == false then
if game.Players:GetPlayerFromCharacter(hit.Parent) then
ch.Debounce.Value = true
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plrItems.item2.Value == "None" then
game.ReplicatedStorage.PlayerRemotesFolder:FindFirstChild(plr.Name.."AddItem"):FireClient(plr, game.ReplicatedStorage.PlayerItems:FindFirstChild(plr.Name.."Items"))
if plrItems.item1.Value == "None" then
plrItems.Choosing.Value = true
else
plrItems.Choosing2.Value = true
end
end
end
end
end
end)
end
end
game.ReplicatedStorage.ClientSendBackItem.OnServerEvent:Connect(function(plr, selected, val, name)
local plrItems = game.ReplicatedStorage.PlayerItems:FindFirstChild(plr.Name.."Items")
local itemVal
if val == 1 then
itemVal = plrItems.item1
plrItems.Choosing.Value = false
else
itemVal = plrItems.item2
plrItems.Choosing2.Value = false
end
itemVal.Value = name
local selected = items[name]
if selected then
itemVal.Uses.Value = selected.Uses
end
end)
game.ReplicatedStorage.ItemUseEvent.OnServerEvent:Connect(function(plr)
local plrItems = game.ReplicatedStorage.PlayerItems:FindFirstChild(plr.Name.."Items")
if plrItems.item1.Value ~= "None" then
local val = plrItems.item1.Value
local selected = items[val]
if selected then
selected.OnUse(plr)
end
plrItems.item1.Uses.Value -= 1
if plrItems.item1.Uses.Value <= 0 then
plrItems.item1.Value = "None"
plrItems.item1.Uses.Value = 0
if plrItems.item2.Value ~= "None" then
plrItems.item1.Name = "item"
plrItems.item2.Name = "item1"
plrItems.item.Name = "item2"
end
game.ReplicatedStorage.PlayerRemotesFolder:FindFirstChild(plr.Name.."SwitchItem"):FireClient(plr)
end
end
end)
game.ReplicatedStorage.ChangeWalkSpeed.Event:Connect(function(speedConfig, walkspeed, waitTime)
print("event")
local defaultSpeed = speedConfig.Value
speedConfig.Value = walkspeed
wait(waitTime)
speedConfig.Value = defaultSpeed
end)
If you find any flaws with my code, please let me know! Thanks, WE
Client side code: