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

Ban system doesn't work. There are no errors in the output. What is wrong?

So I have a gui with a local script for the ban:

local frame = script.Parent.Frame
local button = script.Parent

button.MouseButton1Click:Connect(function()
	if frame.Visible == true then
		frame.Visible = false
	else
		frame.Visible = true
	end
end)

frame.Cancel.MouseButton1Click:connect(function()
    frame.Visible = false
end)

local playerName = frame.Player.Text
local reasoning = frame.Reason.Text

frame.Ban.MouseButton1Click:connect(function()
    if game.Players:FindFirstChild(frame.Player.Text) then
		game.ReplicatedStorage:WaitForChild("Ban"):FireServer(playerName, reasoning)
    end
end)

frame.Unban.MouseButton1Click:connect(function()
	if game.Players:FindFirstChild(frame.Player.Text) then
		game.ReplicatedStorage:WaitForChild("Unban"):FireServer(playerName)
	end
end)

And in SSS, I have a script:

local datastore = game:GetService("DataStoreService"):GetDataStore("BansAndUnbans")

game.Players.PlayerAdded:connect(function(player)
	
	local data = datastore:GetAsync(player.UserId)
	if data then
		if data[1] == true then
			player:Kick("You have been banned. Reason: "..data[2])
		end
	end
end)

game.ReplicatedStorage:WaitForChild("Ban").OnServerEvent:connect(function(player, playerName, reasoning)
	if game.Players:FindFirstChild(playerName) then
		game.Players(playerName):Kick("You have been banned. Reason: "..reasoning)
		datastore:SetAsync(game.Players(playerName).UserId, {true, reasoning})
	end
end)

game.ReplicatedStorage:WaitForChild("Unban").OnServerEvent:connect(function(player, playerName)
	local players = game:GetService("Players")
	local id = players.GetUserIdFromNameAsync(playerName)
	datastore:RemoveAsync(id)
end)

When I test it out, nothing happens. Player doesn't get kicked, nothing in the output.

2 comments

0

there SHOULD be something in the output. line 15 you have a syntax error. when using a variable inside an object value, you denote the beginning of the variable name with brackets ( [] ), not parentheses. the interpreter thinks that you're trying to call an instance value when you do this. Same for line 16

0

game.Players[playerName]:Kick("etc")

1 answer

You are kicking the player and then you attempt to get that player UserId. If the player has been kicked already, you cannot get UserId this way, because player is no longer in Players. So when you attempt to save to datastore, you are not providing valid UserId.

Swap these two lines:

game.ReplicatedStorage:WaitForChild("Ban").OnServerEvent:connect(function(player, playerName, reasoning)
    if game.Players:FindFirstChild(playerName) then
        datastore:SetAsync(game.Players(playerName).UserId, {true, reasoning})

        game.Players(playerName):Kick("You have been banned. Reason: "..reasoning)
    end
end)

Few side notes:

  1. Server does not need to use WaitForChild, unless you create events via scripts (eg. Instatnce.new("RemoteEvent") ).

  2. You should always wrap SetAsync in pcall. Those datastore functions tend to error rather often.

  3. It is better to send UserId through event and then find player using Players:GetPlayerByUserId. If it returns nil, the player already left, but you can still ban him since you have correct UserId.

  4. There is also a function that can get UserId from player name, even if player is not in the game anymore. You can use that instead.

EDIT: Sorry, its Players:GetUserIdFromNameAsync

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