Give Items by Command

  1. What do you want to achieve? Keep it simple and clear!
  • I want to make it so whenever you type “/give PlayerName ItemName Amount” in the chat, it gives the specified player an specified item and the amount. (Just like in Minecraft)
  1. What is the issue? Include screenshots / videos if possible!
  • No screenshots/videos because, it’s just too simple to understand and it doesn’t give me any errors.
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
  • Yeah, nothing helped.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

This is the code i’ve tried.

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		local splitString = string.split(msg," ")
		if msg == "/give" then
			game.ReplicatedStorage.Cheats.GiveItems:Invoke(game.Players:WaitForChild(splitString[2]), splitString[3], splitString[4])
		end
	end)
end)

splitString[2] = PlayerName
splitString[3] = ItemName
splitString[4] = Amount

3 Likes

when you try to print the result of msg after you edited it what does it output?

Let me see.

(rblxcharactes)

Well, nothing shows because it doesn’t even work, i think?

also here did you mean to check for splitString instead of msg?

1 Like

splitString is msg because this way i can check if other words like “/give secondword” appear.

2 Likes

Alright so, when i tried to print the message, it showed me this (the message thing works but it just errors idk why)

And also the “addItem” code.

function addItem(inv,name,count,id)
	if not M_II[name] then return end
	local item = M_II[name]
	count = count or 1
	
	local function rep(i)
		local slot = inv["Slot"..i]
		local tab = S_H:JSONDecode(slot.Value)
		if (tab.count and (tab.count > 0)) and (tab.name == name) then
			local fit = math.min(count,(item.maxstack or 64) - tab.count)
			count = count - fit
			tab.count = tab.count and (tab.count + fit) or fit
			tab.name = name
			slot.Value = S_H:JSONEncode(tab)
			if count <= 0 then
				return true
			end
		end
	end
	local function rep2(i)
		local slot = inv["Slot"..i]
			local tab = S_H:JSONDecode(slot.Value)
			if (tab.count <= 0) or ((tab.count and (tab.count > 0)) and (tab.name == name) or (tab.name == "") or not tab.name) then
				local fit = math.min(count,(item.maxstack or 64) - tab.count)
				count = count - fit
				tab.count = math.max(0,tab.count)
				tab.count = tab.count and (tab.count + fit) or fit
				tab.name = name
				slot.Value = S_H:JSONEncode(tab)
				if count <= 0 then
					return true
				end
			end
	end
	if id then 
		rep2(id)
	end
	if count > 0 then
		for i=0,35 do
			if rep(i) then break end -- do slots that can stack first
		end
	end
	if count > 0 then
		for i=0,35 do
			if rep2(i) then break end
		end
	end
	if count > 0 then
		--spawnItem(name,count)
	end
end

And i also found out “splitString[4]” is the problem, whenever i type a number for the 4th word, it just errors, without it, it works just fine.

nevermind, i just had to use tonumber(splitString[4]) lol

2 Likes

alright glad it worked lol
\\\\\\\\

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