Some commands not working

These admin commands are contained in a module script.
Some of my admin script commands are not working, these are the broken commands:

Commands.fling = function(admin,target)
	if admin and target then
		target = game.Players:FindFirstChild(target)
		if target and target.Character and target.Character:FindFirstChild("Torso") then
			local targ = target.Character.Torso
			local dir = targ.CFrame.lookVector * -1
			local force = Instance.new("BodyVelocity")
			force.velocity = dir * (admin.Character.Torso.Position - targ.Position).magnitude * 10
			force.P = 5000
			force.maxForce = Vector3.new(8e+003, 8e+003, 8e+003)
			force.Parent = targ
			game:service("Debris"):AddItem(force, 6)
			print(admin.Name..' has flinged '..target.Name)
		end
	end
end



Commands.smite = function(admin,target)
	if admin and target then
		target = game.Players:FindFirstChild(target)
		if target and target.Character and target.Character:FindFirstChild("Torso") then
			local targ = target.Character.Torso
			local dir = targ.CFrame.lookVector * -1
			local force = Instance.new("BodyVelocity")
			force.velocity = dir * 100 + Vector3.new(0,20,0)
			force.P = 5000
			force.maxForce = Vector3.new(8e+003, 8e+003, 8e+003)
			force.Parent = targ
			game:service("Debris"):AddItem(force, 6)
			print(admin.Name..' has smited '..target.Name)
		end
	end
end


Commands.kick = function(admin,target,reason)
	if target then
		target = game.Players:FindFirstChild(target)
		if target then
			game.Players:FindFirstChild(target):Kick(reason)
			print(admin.Name..' has kicked '..target.Name)
		end
	end
end



Commands.ban = function(admin,target,reason)
	if target then
		target = game.Players:FindFirstChild(target)
		if target then
			game:GetService("BannedService"):BanUser(target.userId, reason, 0, 0)
			print(admin.Name..' has banned '..target.Name)
		end
	end
end

Commands.fly = function(admin)
	if admin and admin.Character then
		if admin.Character.Humanoid.WalkSpeed < 16 then
			admin.Character.Humanoid.WalkSpeed = 16
			admin.Character.Humanoid.JumpPower = 400
			print(admin.Name..' has turned on fly')
		else
			admin.Character.Humanoid.WalkSpeed = 16
			admin.Character.Humanoid.JumpPower = 400
			print(admin.Name..' has turned off fly')
		end
	end
end

Commands.noclip = function(admin)
	if admin and admin.Character then
		local char = admin.Character
		local primarypart = char:FindFirstChild("Torso") or char:FindFirstChild("UpperTorso")
		if primarypart then
			if not primarypart.Anchored then
				primarypart.Anchored = true
				local weld = Instance.new("Weld")
				weld.Part0 = primarypart
				weld.Part1 = primarypart
				weld.C0 = primarypart.CFrame
				weld.C1 = primarypart.CFrame
				weld.Parent = primarypart
				admin.Character.Humanoid.JumpPower = 400
				print(admin.Name..' has turned on noclip')
			else
				primarypart.Anchored = false
				primarypart.Weld:remove()
				admin.Character.Humanoid.JumpPower = 50
				print(admin.Name..' has turned off noclip')
			end
		end
	end
end

Commands.msgall = function(admin,text)
	if text then
		for i,v in pairs(game.Players:GetChildren()) do
			_G.func(v,text)
		end
		print(admin.Name..' has sent a message to all')
	end
end

Thanks for reading to this point, if you could help me it would be a very big honor, thank you.

  • With regards, YourNameHere.

Have you checked if the function is at least being called correctly?

By this I mean by adding a print as the very first thing it does to detect if it was called.

Alright, doing that right now. I am going to tag you once the result is out.

So most of them are called successfully, except these:

Commands.kick = function(admin,target,reason)
	if target then
		print('Function called successfully.')
		target = game.Players:FindFirstChild(target)
		if target then
			game.Players:FindFirstChild(target):Kick(reason)
			print(admin.Name..' has kicked '..target.Name)
		end
	end
end



Commands.ban = function(admin,target,reason)
	if target then
		print('Function called successfully.')
		target = game.Players:FindFirstChild(target)
		if target then
			game:GetService("BannedService"):BanUser(target.userId, reason, 0, 0)
			print(admin.Name..' has banned '..target.Name)
		end
	end
end

Commands.msgall = function(admin,text)
	if text then
		print('Function called successfully.')
		for i,v in pairs(game.Players:GetChildren()) do
			_G.func(v,text)
		end
		print(admin.Name..' has sent a message to all')
	end
end

@DonKingFrog

That means the target is nil or false. Where do you call the function and what’s collecting the target?

Edit: for msgall the text is then nil or false as well.

It is nil, let me give you the error.:

ServerScriptService.AdminCMDS.Commands:128: attempt to call a nil value
ServerScriptService.AdminCMDS.Commands:105: attempt to index nil with 'Kick'
'BannedService' is not a valid Service name

Commands.kick = function(admin,target,reason)

	if target then
		print('Function called successfully.')
		target = game.Players:FindFirstChild(target)
		if target then
			game.Players:FindFirstChild(target):Kick(reason)
			print(admin.Name..' has kicked '..target.Name)
		end
	end
end
Commands.ban = function(admin,target,reason)
	if target then
		print('Function called successfully.')
		target = game.Players:FindFirstChild(target)
		if target then
			game:GetService("BannedService"):BanUser(target.userId, reason, 0, 0)
			print(admin.Name..' has banned '..target.Name)
		end
	end
end

@DonKingFrog The code is above the reply :slight_smile:

So my suspicion that the target is nil was correct. Could you show me the way you find the target? Also, BannedService isn’t a real service. You have to create your own ban service using datastores.

Could you explain a bit better ( I am not this good of an english speaker when it comes to coding ).

you know how you do:


require(ModuleScript).ban(admin, target, reason)

could you show me how you get admin, target, and reason?

Oh yeah sure, one second…

--AdminCMDS Script to get the requirements.
local commands = require(script.Commands)

Module Script:

Commands.kick = function(admin,target,reason)
local commands = require(script.Commands)

commands.ban() -- Where is this and show me most of the code surrounding this and how you use this within your code. (Not module script)

I only have that code in the module script. But here is the surrounding:

AdminCMDS:

local prefix = '?'

local Admins = {'coolgamer294855'}

local commands = require(script.Commands)

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if table.find(Admins,player.Name) then
			if message:match("^%"..prefix) then
				local args = message:split(" ")
				local command = args[1]:gsub(prefix, "")
				table.remove(args, 1)
				if commands[command] then
					-- Clean Args
					for i, v in pairs(args) do
						if i > 10 then args[i] = nil end
						if tonumber(v) then args[i] = tonumber(v) end
					end
					-- Run Command
					commands[command](player,table.unpack(args))
				end
			end
		end
	end)
end)

Ban Code:

Commands.ban = function(admin,target,reason)
	if target then
		print('Function called successfully.')
		target = game.Players:FindFirstChild(target)
		if target then
			game:GetService("BannedService"):BanUser(target.userId, reason, 0, 0)
			print(admin.Name..' has banned '..target.Name)
		end
	end
end

@DonKingFrog

I’ve gone ahead and looked into your code and come up with a few fixes.

But it’s not a small problem it’s just how the entire script is structured. You’ll have so much trouble using this method so I’d rather just redo it and write it in this form:

Checking script:


This is just an example of what I’d do with the code:

local prefix = '?'

local Admins = {'coolgamer294855'}

local commands = require(script.Commands)

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if table.find(Admins,player.Name:lower()) then
			local args = message:split(" ")
			if message:match("^%"..prefix) then
				local command = args[1]:split(prefix)[2]:lower()
				if commands[command] then
					commands[command](player,args) -- send raw message
				end
			end
			
		end
	end)
end)

Module Script


This is just an example of what I’d do with the code:

local Commands = {}

Commands.ban = function(admin,args)
	local Target = args:split(" ")[2]
	for _, plr in pairs(game.Players:GetPlayers()) do if plr.Name:lower() == Target:lower() then Target = plr end end -- Finds player
	
	local Reason = table.remove(args, 1) and table.remove(args, 2) -- Gets reason

	--Ban him here
end

Commands.kick = function(admin, args)
	local Target = args:split(" ")[2]
	for _, plr in pairs(game.Players:GetPlayers()) do if plr.Name:lower() == Target:lower() then Target = plr end end -- Finds player
	
	-- Kick here
end

return Commands

Thank you very much for helping!

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