Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

For some reason when ever I chat I die, any ideas?

I'm currently making a custom admin script and it was all going well until I realised that everything I said killed me. This is my script:

local Admins = {"robloxianmirror", "Player", "Player1"}
local Prefix = "#"
local Banned = {"Bad_Guy123"}

--[[
	"Me" isn't implemented yet however "[Prefix]kill all" will work
	
	Current commands:
		Manual Ban Via script
		shutdown - Shutsdown the server
		kill - Kills a player
		damage - Takes of 10% of a player's health
--]]

game.Players.PlayerAdded:connect(function(Player) -- Bans
	for l, a in pairs(Banned) do
		if Player.Name == Banned[l] then
			Player:Kick("You are banned from this game.")
		end
	end
end)

game.Players.PlayerAdded:connect(function(Player) -- Check for Admin
	for i, v in pairs (Admins) do
		if Player.name == Admins[i] then
			local Location = Player.PlayerGui
			local Gui = script.ScreenGui:Clone()
			Gui.Parent = Location
			Gui.Frame.Text.Text = "Welcome " .. Player.Name .. ", you are an admin!"
			Gui.Frame:TweenPosition(UDim2.new(0.333, 0 ,0 ,0), "In", "Quad", 0.75)
			wait(3)
			Gui.Frame:TweenPosition(UDim2.new(1.2, 0, 0, 0), "Out", "Quad", 0.75)
			wait(0.75)
			Gui:Remove()
			Player.Chatted:connect(function(msg)
				if msg == Prefix .. "kill all" or Prefix .. "reset all" then
					local Players = game.Players:GetPlayers()
					for i, v in pairs (Players) do
						Players[i].Character.Head:Remove()
					end
				end
				if string.sub(msg, 1, string.len(Prefix .. "kill ")) == Prefix .. "kill " then
					local info = string.sub(msg, string.len(Prefix .. "kill "))
					if game.Workspace:FindFirstChild(info) ~= nil then
						local Victim = game.Workspace:FindFirstChild(info)
						Victim.Head:Remove()
					end
				end
				if string.sub(msg, 1, string.len(Prefix .. "reset ")) == Prefix .. "reset " then
					local info = string.sub(msg, string.len(Prefix .. "reset "))
					if game.Workspace:FindFirstChild(info) ~= nil then
						local Victim1 = game.Workspace:FindFirstChild(info)
						Victim1.Head:Remove()
					end
				end
				if msg == Prefix .. "shutdown" then
					game.Players:ClearAllChildren()
					game.Workspace:ClearAllChildren()
				end
				if string.sub(msg, 1, string.len(Prefix .. "damage ")) == Prefix .. "damage " then
					local info = string.sub(msg, string.len(Prefix .. "damage "))
					if game.Workspace:FindFirstChild(info) ~= nil then
						local Victim2 = game.Workspace:FindFirstChild(info)
						Victim2.Humanoid.Health = Victim2.Humanoid.Health + 10
					end
				end
			end)
		end
	end
end)

2 answers

✓ Accepted answer

This is because you are using the or operator incorrectly.

On line 36, you put: if msg == Prefix .. "kill all" or Prefix .. "reset all" then. That would always be true, no matter what.

The if statement works like this:

If the value between if and then is truthy then it will run the code, otherwise, it won't. The only non-truthy values are false and nil All other values, like strings, tables functions, (even if they are empty, or the number is 0) are truthy

msg == Prefix.. "kill all" - that is fine, due to the fact that if msg wasn't the prefix followed by "kill all", the value will be false, which is a non-truthy value, so it will work as expected. "#reset all" is always a truthy value because it's a string, and since you didn't put msg == in front of it, it doesn't evaluate whether msg is #reset all, which would return true or false, it would just read the truthy string value and decide that since it's neither false nor nil, it executes the code.

The short answer is that you have to put "msg ==" every time.

if msg == Prefix .. "kill all" or msg == Prefix .. "reset all" then
	-- code
end

Also, my 100th answer! Hooray!

1 comment

0

Thanks, I kind of rushed it. That's probably why.

Friend Me On Roblox And We Can Work Out Together About This Lua Problem

1 comment

0

I'd rather not, sorry.

This discussion is preserved as it was. New answers, comments, and votes are closed.