How do I fire a event from a LocalScript to enable a ServerScript in workspace?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to know how I can fire an event from a LocalScript so a script in workspace will be enabled.

  2. What is the issue? I don’t know how to enable a ServerScript from a LocalScript event.

  3. What solutions have you tried so far? I tried using a RemoteEvent which didn’t work.

Local:

local Button = script.Parent
local CodeBox = Button.Parent.TextBox

Button.MouseButton1Up:Connect(function()
	if CodeBox.Text == "Test" then
		wait(2)
		Button.Parent.Parent:Destroy()
	else
		game.Players.LocalPlayer:Kick("Access denied.")
	end
end)
1 Like

Remote Events should be used for this. Create a remote event in ReplicatedStorage, and name it whatever you want (just remember to put the exact name of it in the script). In your LocalScript, you want to use the FireServer() function to send a signal to the server, and let the server do the rest of the work using the OnServerEvent event.

There’s a lot to know about Remote Events, so I suggest you read more about them here

Local Script example:

local Button = script.Parent
local CodeBox = Button.Parent.TextBox

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- Remote Event name here
Button.MouseButton1Up:Connect(function()
	if CodeBox.Text == "Test" then
		task.wait(2)
		Button.Parent.Parent:Destroy()
	else
		Event:FireServer(false) -- Fires the remote event to the server
	end
end)

Server Script example:

local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- RemoteEvent name here

Event.OnServerEvent:Connect(function(player, boolean) -- receives the signal
	if not boolean then -- if 'boolean' is 'false'
		player:Kick("Access denied.") -- kicks player
	end
end)

I am not sure what you are trying to achieve, but this is all I could do for now.

1 Like

Thank you, I will try this !!!
I am also trying to make a code which upon you spawn, if you get the code wrong, it kicks the player. if its right, i want the script in workspace to be enabled

@BabyNinjaTime I don’t think your idea will work. Here’s what im trying to do:
image

In the LocalScript, I want it so that after it removes the GUI (if player gets it correct) i want to enable the white outlined script in workspace. So if 1 person get’s the code, the game will start from the Main script.

I didn’t test it, but it should if I am correct:

Make a RemoteEvent and put it in ReplicatedStorage

Local Script:

local Button = script.Parent
local CodeBox = Button.Parent.TextBox
local Event = game.ReplicatedStorage.RemoteEvent

Button.MouseButton1Up:Connect(function()
	if CodeBox.Text == "Test" then
		wait(2)
		Button.Parent.Parent:Destroy()
                Event:FireServer()
	else
		game.Players.LocalPlayer:Kick("Access denied.")
	end
end)

Server Script (in ServerScriptService):

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
    game.Workspace.Main.Enabled = true
end)
1 Like

A more secure way to establish the connection would be to do the comparison on the server:

--Client
RemoteEvent:FireServer(TextBox.Text)
--Service
RemoteEvent.OnServerEvent:Connect(function(player, txt)
   if (txt ~= "Test") then
      player:Kick("Access denied.")
   end
end)
1 Like

Thank you. I also have a more clear understanding of how to use “events”.

1 Like

This isn’t an ideal solution as there are still some issues with the code (not something you would probably notice while testing). One main thing you need to change is you need to kick the player from the server not the client.

Here is your fixed code:

LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("RemoteEvent") -- RemoteEvent name here

local Button = script.Parent
local CodeBox = Button.Parent.TextBox

Button.Activated:Connect(function() -- Changed to 'Activated'
	if CodeBox.Text == "Test" then
		task.wait(2) -- 'wait()' changed to 'task.wait()' as it's a better 'wait()' function
		Button.Parent.Parent:Destroy()
		Event:FireServer(CodeBox.Text) -- Fires the remote event
	else
		Event:FireServer(CodeBox.Text)
	end
end)

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("RemoteEvent")

local serverScript = workspace:WaitForChild("Main")

Event.OnServerEvent:Connect(function(plr, text) -- 'text' is the text sent from the LocalScript that fired the RemoteEvent
	if text == "Test" then -- if the text is "Test"
		serverScript.Enabled = true -- enables the 'Main' server Script
	else -- if 'text' is not "Test"
		plr:Kick("Access denied.") -- kicks player
	end
end)

Did you even test your code? I’m getting an error.
image

EDIT: I had to use OnServerEvent.

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