Give Item Command Does Not Work

I am trying to make a give item command for a restaurant game and the give item script does not work

local Players = game:GetService("Players")

Players.PlayerAdded:connect(function(Player)
	
		Player.Chatted:Connect(function(msg)
			local SplitString = msg:split(" ")
			
		if SplitString[1] == ";give" then
			warn("yes")
			print(SplitString[2])
			local bruh = workspace:FindFirstChild(SplitString[2])
			local ahahha = game.Players:GetPlayerFromCharacter(bruh)
			if ahahha == true then
				print("found player")
				local andd = Player.Character:FindFirstChild("BloodBot"):Clone()
				andd.Parent = ahahha:FindFirstChild("Backpack")
				Player.Character:FindFirstChildWhichIsA("Tool"):Destroy()
			elseif ahahha == false then
				warn("no work :(")
				end
				
		end
		if SplitString[1] == ";speed" then
			local bruh = SplitString[2]
			Player.Character:FindFirstChild("Humanoid").WalkSpeed = SplitString[2]

		end
			
		end
		)
	
	end)

help is appreciated

There is multiple issues and bad practices within this script.

  • Index 2 in SplitString would be the player when setting the speed.
  • Trying to set a int as the characters WalkSpeed
  • Incorrect indenting
  • Unnecessary lines

Here is a script that fixes all of that:

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		local Command = string.split(Message, " ")[1]
		local Target = game.Players:FindFirstChild(string.split(Message, " ")[2])

		if Target then
			if Command == ";give" then
				local Tool = game.Workspace[Player.Name].TestTool
				Tool.Parent = Target.Backpack
			elseif Command == ";speed" then
				Target.Character:WaitForChild("Humanoid").WalkSpeed = tonumber(string.split(Message, " ")[3])
			end
		else
			print("Player has left the game")
		end
	end)
end)
1 Like