Not able to limit a function from spawning

I have been struggling really hard to limit the script from spawning a certain amount of objects for each player.
I have created a script that allows to spawn objects from a GUI via local script > remote event
but I want to give each player a limit of 5 objects

What do you want to achieve? Limit the player from spamming objects

What solutions have you tried so far? I looked far and wide, I have tried for i,v ipairs, pairs(t), now if and elseif statements. math.clamps,
attribute for limit and attribute for how many spawned (latter not updating when the player spawns an object))

trying for loops either doesn’t give the expected result or pauses rendering and gives me an error
attempt to get length of a instance value

for now this is the current code sample with the method i am trying

local function giveCarrierStat(Players)
	Players:SetAttribute("isCarrying", false)
	Players:SetAttribute("cargoLimit", 5)
	local cargoLimit = Players:GetAttribute("cargoLimit", 5)
	local playerCargoFolder = Instance.new("Folder", cargoStorage)
	playerCargoFolder.Name = Players.Name
	local cargAmount = playerCargoFolder:GetChildren()
	local playerGui = Players:WaitForChild("PlayerGui")
	if #cargAmount < cargoLimit then
		local function instantiateCrate(Players, parent, partPosition, value, destination, spawner)
			local newCargo = newCrate:Clone()
			newCargo.Parent = cargoStorage:FindFirstChild(Players.Name)
			newCargo.Position = partPosition
			newCargo:SetAttribute("Value", value)
			newCargo:SetAttribute("Destination", destination)
			newCargo:SetAttribute("Spawner", spawner)
		end
		insC.OnServerEvent:Connect(instantiateCrate)
	elseif #cargAmount > cargoLimit then
		local Warn = playerGui:FindFirstChild("WarnLimit")
		Warn.Enabled = true
		wait(3)
		Warn.Enabled = false	
	end
end
Players.PlayerAdded:Connect(giveCarrierStat)

thank you everyone inadvance

do u get any errors when u run it or does it just not work, i think you could just use an else statement instead of adding the #cargAmount thing after, if it doesnt work u could maybe check the limit first, then run the other stuff, other than that it looks okay to me

1 Like

the solution was so easy i am dumbfounded lol, used else and made the localscript gui just check lol

heck yea there u go, good job!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.