You can write your topic however you want, but you need to answer these questions:
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.
What is the issue? I don’t know how to enable a ServerScript from a LocalScript event.
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)
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.
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:
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.
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)
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)