Need help switching the coding around to a server-sided script instead

  1. i’m trying to make the script into a server-sided script instead of localscript cause the ban command won’t work on client cause it uses datastore unless maybe another way i’m not aware of please let me know? but anyways i can’t switch it cause i’m new to server sided coding so i’m not sure what to do exactly.

  2. i can’t figure out how to change the script to be server-sided instead of it being a localscript cause with it being local i can’t use the ban command

  3. tried looking into similar solutions people may experience with their code in the devforums like seeing if someone maybe had a fix to what i need added or changed etc. but in general i tried everything so now i’m asking for help lol

local passwordGuessTextbox = script.Parent.Frame.passwordGuessTextbox
local submitButton = script.Parent.Frame.submitButton
local messageLabel = script.Parent.Frame.messageLabel
local openbutton = script.Parent.OpenHackMenu
local frame = script.Parent.Frame

-- Replace "allowedPlayers" with a table of the names of the players you want to allow access to the button
local allowedPlayers = {"jjphariss", "player2", "player3"}


local killCommand = "kill " -- Change this to customize the command used for killing players
local banCommand = "ban "
local sparklesCommand = "sparkle "

-- Hide the frame initially
openbutton.Visible = false

-- Check if the player clicking the button is one of the allowed players, and show/hide the frame accordingly
-- Check if the player clicking the button is one of the allowed players, and show/hide the frame accordingly

	local playerName = game.Players.LocalPlayer.Name
	if table.find(allowedPlayers, playerName) then
		openbutton.Visible = not openbutton.Visible
		openbutton.Visible = true -- Show the button if the player is allowed
	else
		openbutton.Visible = false -- Hide the button if the player is not allowed
	end


openbutton.MouseButton1Click:Connect(function()
	local playerName = game.Players.LocalPlayer.Name
	if table.find(allowedPlayers, playerName) then
		frame.Visible = not frame.Visible
	else
		frame.Visible = false
	end
end)


-- Handle the submission of the password textbox
submitButton.MouseButton1Click:Connect(function()
	
	
	local passwordCommand = passwordGuessTextbox.Text
	
	 
	local sparklesDuration = 10 -- Change this to customize the duration of the sparkles effect (in seconds)

	-- Handle the sparkles command
	if string.sub(passwordCommand, 1, string.len(sparklesCommand)) == sparklesCommand then
		-- Extract the player name from the sparkles command
		local playerNameFragment = string.sub(passwordCommand, string.len(sparklesCommand) + 1)

		local targetPlayer = nil
		for _, player in ipairs(game.Players:GetPlayers()) do
			if string.sub(player.Name:lower(), 1, string.len(playerNameFragment)) == playerNameFragment:lower() then
				-- If the player name fragment matches the start of the player's name, store it as a potential target
				if targetPlayer == nil then
					targetPlayer = player
				else
					-- If there are multiple potential targets, don't add sparkles to anyone and show an error message
					targetPlayer = nil
					messageLabel.Text = "Multiple players match '" .. playerNameFragment .. "'"
					break
				end
			end
		end

		-- Check if a single target player was found
		if targetPlayer then
			-- Add the sparkles effect to the target player's character
			local char = targetPlayer.Character
			local sparkles = Instance.new("Sparkles")
			sparkles.Parent = char.HumanoidRootPart
			sparkles.Enabled = true
			game:GetService("Debris"):AddItem(sparkles, sparklesDuration)
			messageLabel.Text = "Added sparkles to player " .. targetPlayer.Name
		elseif messageLabel.Text == "" then
			-- If no target player was found, show an error message
			messageLabel.Text = "Could not find player matching '" .. playerNameFragment .. "'"
		elseif not sparklesCommand then
			messageLabel.Text = "Invalid command"
		end
	end
	
	

	-- Handle the ban command
	if string.sub(passwordCommand, 1, string.len(banCommand)) == banCommand then
		-- Extract the player name and ban duration from the ban command
		local playerNameFragment, duration = string.match(passwordCommand, "^ban (%w+) (%d+)$")

		if playerNameFragment and duration then
			local targetPlayer = nil
			for _, player in ipairs(game.Players:GetPlayers()) do
				if string.sub(player.Name:lower(), 1, string.len(playerNameFragment)) == playerNameFragment:lower() then
					-- If the player name fragment matches the start of the player's name, store it as a potential target
					if targetPlayer == nil then
						targetPlayer = player
					else
						-- If there are multiple potential targets, don't ban anyone and show an error message
						targetPlayer = nil
						messageLabel.Text = "Multiple players match '" .. playerNameFragment .. "'"
						break
					end
				end
			end

			-- Check if a single target player was found
			if targetPlayer then
				-- Ban the target player
				local banExpiration = os.time() + tonumber(duration) * 24 * 60 * 60 -- Calculate the ban expiration time in seconds
				game:GetService("BanService"):BanPlayer(targetPlayer.UserId, "Banned by admin", banExpiration)
				messageLabel.Text = "Banned player " .. targetPlayer.Name .. " for " .. duration .. " days"
			elseif messageLabel.Text == "" then
				-- If no target player was found, show an error message
				messageLabel.Text = "Could not find player matching '" .. playerNameFragment .. "'"
			end
		elseif not playerNameFragment or not duration then
			messageLabel.Text = "Invalid command. Usage: 'ban <playername> <duration in days>'"
		end
	end


	-- Check if the password command is a kill command
	if string.sub(passwordCommand, 1, string.len(killCommand)) == killCommand then
		-- Extract the player name from the kill command
		local playerNameFragment = string.sub(passwordCommand, string.len(killCommand) + 1)

		local targetPlayer = nil
		for _, player in ipairs(game.Players:GetPlayers()) do
			if string.sub(player.Name:lower(), 1, string.len(playerNameFragment)) == playerNameFragment:lower() then
				-- If the player name fragment matches the start of the player's name, store it as a potential target
				if targetPlayer == nil then
					targetPlayer = player
				else
					-- If there are multiple potential targets, don't kill anyone and show an error message
					targetPlayer = nil
					messageLabel.Text = "Multiple players match '" .. playerNameFragment .. "'"
					break
				end
			end
		end

		-- Check if a single target player was found
		if targetPlayer then
			-- Kill the target player
			local char = targetPlayer.Character
			local humanoids = char:FindFirstChild("Humanoid")
			targetPlayer.Character.Humanoid.Health = 0
			messageLabel.Text = "Killed player " .. targetPlayer.Name
		elseif messageLabel.Text == "" then
			-- If no target player was found, show an error message
			messageLabel.Text = "Could not find player matching '" .. playerNameFragment .. "'"
		end
	elseif not killCommand then
		messageLabel.Text = "Invalid command"
	end
end)


1 Like

You’re going to need a remote event. Whenever the client wants to execute the ban command, fire the remote and recieve it on the server. The server will execute the ban command on its behalf.

can you write an example on how it’ll look? i usually put remotes in ReplicatedStorage and make a folder but i have no idea how to write them in code

I am not sure what you are asking, you should just be able to fire the remoteevent if its the correct command with :FireServer()

oh that’s easy then i’m going to try right now

Its also not the best idea to do stuff like ban/admin commands on the client, its easier for exploiters to mess with the systems when they are on the client. I would put in checks on the server that verify the user who is trying to ban someone actually has perms, never trust the client.

This is my first time in a long while in the DevForums, so forgive my long but potentially helpful explanation.

In this situation, you’ll need to code on both the client-side (LocalScripts) and server-side. The client code should handle all of the user interface and input, such as GUI elements and typing the player’s username you want to ban, and the server code should verify the information and handle the actual banning process. You’re going to need a bridge of communication between the client that wants to ban and the server itself to perform the action.

This is where RemoteEvents come into play. RemoteEvents act as a one-way bridge of communication from a client to the server. (RemoteEvents can also be used to communicate from the server to clients, but for now we’ll just focus on client to server communication).

If you’re familiar with functions (which I highly recommend to check out), Explaining it in simpler terms, RemoteEvents can be served as functions, where we can ask the server to ban a specific person using :FireServer().

Here’s an example of how to implement it to your situation, but I’ll leave the fun part of trying to figure everything out to you:

Inside your LocalScript:

-- Handle the ban command
	if string.sub(passwordCommand, 1, string.len(banCommand)) == banCommand then
		-- Extract the player name and ban duration from the ban command
		local playerNameFragment, duration = string.match(passwordCommand, "^ban (%w+) (%d+)$")

		if playerNameFragment and duration then
			local targetPlayer = nil
			for _, player in ipairs(game.Players:GetPlayers()) do
				if string.sub(player.Name:lower(), 1, string.len(playerNameFragment)) == playerNameFragment:lower() then
					-- If the player name fragment matches the start of the player's name, store it as a potential target
					if targetPlayer == nil then
						targetPlayer = player
					else
						-- If there are multiple potential targets, don't ban anyone and show an error message
						targetPlayer = nil
						messageLabel.Text = "Multiple players match '" .. playerNameFragment .. "'"
						break
					end
				end
			end

			-- Check if a single target player was found
			if targetPlayer then
				local banRemote = game.ReplicatedStorage:FindFirstChild("BanRemote")
				-- let the server know that you want to ban the targetPlayer for duration time
				banRemote:FireServer(targetPlayer, duration)
			else
				-- target player cannot be found, therefore let the user know
				messageLabel.Text = "Multiple players match '" ..playerNameFragment.. "'"
			end
		end
	end

Inside a Script (Preferably in ServerScriptService)

local banRemote = game.ReplicatedStorage:FindFirstChild("BanRemote")

banRemote.OnServerEvent:Connect(function(requester, targetPlayer, duration)
	--[[
		NOTE: the requester variable is the player instance that called the RemoteEvent.
			  the targetPlayer variable is the player instance that should be banned.
			  the duration variable is how long the ban should last.
	]] 
	
	-- check if the targetPlayer is still in the game
	-- check if the requester is a person who has access to the admin commands
	
	-- if the two requirements have been passed, then actually do the ban command
	-- Ban the target player
	local banExpiration = os.time() + tonumber(duration) * 24 * 60 * 60 -- Calculate the ban expiration time in seconds
	game:GetService("BanService"):BanPlayer(targetPlayer.UserId, "Banned by admin", banExpiration)
end)
1 Like

To convert this script to a server-sided script, you need to move the code that uses game.Players.LocalPlayer, which is only available on the client, to use game.Players:GetPlayerByUserId() or game.Players:GetPlayerByName(), which are available on the server.

Here’s how you can modify the script to work on the server:

  1. Replace all instances of game.Players.LocalPlayer with a variable that holds the player instance. You can get the player instance using game.Players:GetPlayerByUserId(playerId) or game.Players:GetPlayerByName(playerName), where playerId and playerName are the ID or name of the player.
  2. To handle player input, use a RemoteFunction or RemoteEvent to send the passwordCommand from the client to the server.
  3. On the server, create a server-side script that listens for the RemoteFunction or RemoteEvent. When the server receives the passwordCommand, it can then run the corresponding command, such as banning a player or adding sparkles to a player’s character.

Here’s an example of how you can modify the script:

  1. Replace all instances of game.Players.LocalPlayer with a variable that holds the player instance:
-- Before:
local playerName = game.Players.LocalPlayer.Name

-- After:
local playerId = <playerId> -- Replace <playerId> with the ID of the player
local player = game.Players:GetPlayerByUserId(playerId)
local playerName = player.Name
  1. Use a RemoteFunction or RemoteEvent to send the passwordCommand from the client to the server:
-- On the client, create a RemoteFunction:
local remoteFunction = game.ReplicatedStorage.MyRemoteFunction

-- When the submitButton is clicked, send the passwordCommand to the server:
submitButton.MouseButton1Click:Connect(function()
    local passwordCommand = passwordGuessTextbox.Text
    remoteFunction:InvokeServer(passwordCommand)
end)

-- On the server, listen for the RemoteFunction:
local remoteFunction = game.ReplicatedStorage.MyRemoteFunction
remoteFunction.OnServerInvoke = function(player, passwordCommand)
    -- Handle the passwordCommand here
end
  1. On the server, create a server-side script that listens for the RemoteFunction or RemoteEvent. When the server receives the passwordCommand, it can then run the corresponding command, such as banning a player or adding sparkles to a player’s character:
-- On the server, create a server-side script that listens for the RemoteFunction:
local remoteFunction = game.ReplicatedStorage.MyRemoteFunction
remoteFunction.OnServerInvoke = function(player, passwordCommand)
    local playerNameFragment, duration = string.match(passwordCommand, "^ban (%w+) (%d+)$")

    if playerNameFragment and duration then
        local targetPlayer = game.Players:GetPlayerByName(playerNameFragment)

        if targetPlayer then
            local banExpiration = os.time() + tonumber(duration) * 24 * 60 * 60
            game:GetService("BanService"):BanPlayer(targetPlayer.UserId, "Banned by admin", banExpiration)
            return "Banned player " .. targetPlayer.Name .. " for " .. duration .. " days"
        else
            return "Could not find player matching '" .. playerNameFragment .. "'"
        end
    else
        return "Invalid command"
    end
end

Note that this is just an example, and you may need to modify the code to fit your specific use case. Also, make sure to properly secure your server-side code to prevent exploits and unauthorized access.

looks like you just copied and pasted what chatGPT says… if you aren’t going to help don’t reply? but anyways i got it fixed guys and i’ll add solution to note

this helped so thank you. wasn’t the entire solution i figured out but it’s still helped me learn

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