Event not firing inside of a function

I’m having a issue where this event is not firing if I put it inside a function, but if I run it at the beginning of the script it works perfectly fine. The code of the function is also running everything but firing the event.

The function gets fired later down the script


local function onKickButtonClicked()
	if selectedPlayerId ~= nil then
		local playerToKick = game.Players:GetPlayerByUserId(selectedPlayerId)
		if playerToKick then
			remote:FireServer((game.Players.LocalPlayer.Name .. " kicked " .. tostring(playerToKick)), 
            (kickReason.Text), game.Players.LocalPlayer.Name)
			playerToKick:Kick(game.Players.LocalPlayer.Name .. " kicked you because ".. 
            kickReason.Text)
			print("Kicked " .. selectedPlayerId)
		else
			warn("Could not find player to kick.")
		end
	else
		warn("No player selected")
	end
end
2 Likes

Try taking it out of the parentheses. I mean the double parentheses. Also have you tested to make sure that it’s going through? Print statements and checking the console?

I tested running it at the beginning of the script and it works fine.

remote:FireServer(game.Players.LocalPlayer.Name, kickReason.Text)

So does the event fire? Make a print statement that prints out every single part it shows.

Is this a local script? Just asking.

Its not something wrong with the code. I pasted it at the top of the script and it ran fine. Its only not running inside of this function.

Yes its being fired in a localscript

Then you will need to use FireClient, from my understanding.

then show entire code, so that we can fully understand whats going on before and after

You cannot use :FireClient() inside a localscript

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local remote = ReplicatedStorage:WaitForChild("SendWebhook")


local ui = script.Parent
local Main = ui.AdminPanelMain.Main
local kickReason = Main.KickText

local SelectionHandler = require(game:GetService("StarterGui"):WaitForChild("OptionSelectedHandler"))

local textbox = Main.Username

local userId
local thumbType = Enum.ThumbnailType.AvatarBust
local thumbSize = Enum.ThumbnailSize.Size420x420


local selectedPlayerId = nil


local function onEnterPressed(inputObject, gameProcessedEvent)

	if inputObject.KeyCode == Enum.KeyCode.Return then

		local playerName = textbox.Text

		local success, result = pcall(function()
			return game:GetService("Players"):GetUserIdFromNameAsync(playerName)
		end)


		if success then
			local content = game.Players:GetUserThumbnailAsync(result, thumbType, thumbSize)
			userId = result
			selectedPlayerId = result
			Main.UserAvatar.Image = content
			print(tonumber(result))
		else
			Main.nonefound.Text = ("Failed to get user ID for " .. playerName)
			Main.nonefound.Visible = true
			warn("Failed to get user ID for " .. playerName)
			wait(2)
			Main.nonefound.Visible = false
			Main.nonefound.Text = "Label"
		end
	end
end


local gameProcessedEventEvent = Instance.new("BindableEvent")
gameProcessedEventEvent.Event:Connect(onEnterPressed)


textbox.FocusLost:Connect(function(inputObject, gameProcessedEvent)
	gameProcessedEventEvent:Fire(gameProcessedEvent)
end)


local function onKickButtonClicked()
	if selectedPlayerId ~= nil then
		local playerToKick = game.Players:GetPlayerByUserId(selectedPlayerId)
		
		if playerToKick then
			remote:FireServer((tostring(game.Players.LocalPlayer.Name) .. " kicked " .. tostring(playerToKick)), (kickReason.Text), game.Players.LocalPlayer.Name)
			playerToKick:Kick(game.Players.LocalPlayer.Name .. " kicked you because ".. kickReason.Text)
			print("Kicked " .. selectedPlayerId)
		else
			warn("Could not find player to kick.")
		end
	else
		warn("No player selected")
	end
end


local function onBanButtonClicked()
	if selectedPlayerId ~= nil then
		local playerToKick = game.Players:GetPlayerByUserId(selectedPlayerId)
		if playerToKick then
			playerToKick:Kick("You have been banned by "..game.Players.LocalPlayer.Name.. " for" .. kickReason.Text)
			print("Banned " .. selectedPlayerId)
		else
			warn("Could not find player to kick.")
		end
	else
		warn("No player selected")
	end
end


local function onWarnButtonClicked()
	if selectedPlayerId ~= nil then
		print("Warned " .. selectedPlayerId)
	else
		warn("No player selected")
	end
end

local kickButton = Main.Kick
local banButton = Main.Ban
local warnButton = Main.Warn



kickButton.MouseButton1Click:Connect(onKickButtonClicked)
banButton.MouseButton1Click:Connect(onBanButtonClicked)
warnButton.MouseButton1Click:Connect(onWarnButtonClicked)







	
1 Like

too many lines of code for me to compute (my laziness kicking in),
somebody else can solve the issue… bye

do you know that the event isnt being fired? try to print something at the beginning of the part where the server script recieves and see if it works, if not tell me

I figured it out I used a Remote Function and that fixed the issue.

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