(Sorry for the last topic, I accidentally clicked create topic)
I’m trying to make a collectible item that’s only visible on the client-side, just like Grass Cutting Incremental, which has each person having their own field and own grass to cut specific to them.
What I want to do is spawn blocks client-sided, so when a player touches a block, it adds a coin. It works fine like i want, but there are 3 problems:
- When there are over 3-4k blocks the game starts to lag, collects it after 2-3 seconds
- Table gaps (never seen it before), like this:
--[1] = ▶ {...},
--[2] = ▶ {...},
--[3] = ▶ {...},
--[4] = nil,
--[5] = nil,
--[6] = ▶ {...},
--[7] = ▶ {...},
--[8] = ▶ {...},
--[9] = ▶ {...},
--[10] = ▶ {...},
- The way i’m doing is it exploitable?
Server:
--// Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--// Variables
local blocks = {}
local function BlockAmount(player)
local number = 0
for ID, info in pairs(blocks[player.UserId]) do
number+=1
end
return number
end
Players.PlayerAdded:Connect(function(player)
-- adds player to the block's table
blocks[player.UserId] = {}
-- create leaderstats
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
local cash = Instance.new("NumberValue")
cash.Name = "Cash"
cash.Parent = folder
folder.Parent = player
-- block's loop
task.spawn(function()
local ID = 0
while player.Parent do
if BlockAmount(player) < 4000 then
ID += 1
-- block info
local block = {}
block.Type = "Basic"
block.ID = ID
block.Position = Vector3.new(math.random(-50, 50), 2, math.random(-50, 50))
block.Value = 10
-- store block info
table.insert(blocks[player.UserId], block)
-- tell the player to spawn a block
ReplicatedStorage.Remotes.Spawn:FireClient(player, block)
end
task.wait()
end
end)
end)
Players.PlayerRemoving:Connect(function(player)
-- destroy player block's
blocks[player.UserId] = nil
end)
ReplicatedStorage.Remotes.Collect.OnServerInvoke = function(player, pos, ID)
for block, info in pairs(blocks[player.UserId]) do
if info.ID == ID then
if info.Position == pos then
player.leaderstats.Cash.Value += info.Value
blocks[player.UserId][block] = nil
return true
end
end
end
return false
end
Client:
--// Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
ReplicatedStorage.Remotes.Spawn.OnClientEvent:Connect(function(blockInfo)
-- create block
local block = ReplicatedStorage.Blocks:FindFirstChild(blockInfo.Type):Clone()
block.Position = blockInfo.Position
block.Parent = workspace.Blocks
-- when player touches block then
block.Touched:Connect(function(hit)
local character = hit.Parent:FindFirstChild("Humanoid")
if character then
-- make sure the block is in the same position as it was originally generated
local canCollect = ReplicatedStorage.Remotes.Collect:InvokeServer(block.Position, blockInfo.ID)
if canCollect then
if block then
block:Destroy()
end
end
end
end)
end)