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

Scripts work in studio but not servers?

These twitter GUIs work in studio but not in servers. Here's the first script (A script in Workspace, with a "Wait" (remote event))

game.Players.PlayerAdded:connect(function()
    shared.TwitterStatus = false
    game.Workspace:WaitForChild("Wait"):FireServer()
    end)--  this goes into the script inside of workspace

Second script in "Submit" inside of a ScreenGui inside of a frame. (local script)

SCRIPT:

--put this script into a local script in the place of the old one that didnt work
local gear = game.ReplicatedStorage:WaitForChild("GravityCoil")
	game.Workspace.Wait.OnServerEvent:connect(function()
		shared.TwitterStatus = false -- now that we are using it, it is no longer nil and has a value.
    --It is also setting the variable once when the player is added to prevent it from being added again when player respawns
			script.Parent.MouseButton1Click:connect(function()
   			 if script.Parent.Parent.Input.Text == 'Alpha!' and shared.TwitterStatus == false then
	print("fire")
	
         		 shared.TwitterStatus = true -- now that it is true, it cannot be ran in this condition again. But as i said before, it can be edited from any script, so u can change it later
       			 gear:Clone().Parent = game.Players.LocalPlayer.Backpack
            script.Parent.Parent.Input.Text = ''
       			 script.Parent.Text = 'Success!'
        wait(1)
       			 script.Parent.Text = 'Submit'
    else
       			 script.Parent.Parent.Input.Text = 'Submit'
        			script.Parent.Text = 'Invalid Code'
        wait(1)
        			script.Parent.Text = 'Submit'
    end

	end)
end)--this script will go into a local script in the place of the old one that didnt work

1 answer

✓ Accepted answer

This may or may not be the answer you're looking for, but by looking at it I can tell you what's wrong.

1: You cannot fire remote events from a server script. That should only be done locally. If you wish to fire an event from a server script, use a BindableEvent instead.

2: The global "shared" table (just like _G), is separate from server and clients. For example:

Saying:

-- Server script

shared.Test = "Testing"

Then...

-- Client-sided

print(shared.Test) -- > nil

3: You cannot create an event from a local script using "OnServerEvent". The correct procedure for making the client listen for server instruction is the "OnClientEvent", which is the event that will fire when "FireClient" or "FireAllClients" is called.

4: I'd also recommend using a Remote Function object to exchange data between clients and server, in place of the "shared" table, which is separate for clients and servers.

5: Also, you probably shouldn't put another event inside an event (unless you have some kind of managing system that properly controls connections / disconnections). So you should take the "MouseButton1Click" event out of the remote event fuction.

Other than that, everything else seems to be fine, but I haven't tested any of this. Anything else that errors will depend on the condition in which the environment of your script is running is in (i.e, FilteringEnabled being on, something missing in the workspace, etc).

You should also probably note that everything in studio runs locally. So you will most likely always have different results in studio than online mode.

For more information on how this works, check out this roblox wiki tutorial: http://wiki.roblox.com/index.php?title=RemoteFunction_and_RemoteEvent_Tutorial

EDIT

Okay so here's an example:

Server script:

--\\ Services
local Players = game:GetService'Players'

--\\ Remote
local Remote = workspace:WaitForChild'Wait'

--\\ Connection
Players.PlayerAdded:connect(function(Player)
	Remote:FireClient(Player,'Test') -- Remember, 'test' was our "Request" argument. This will toggle the "Status" variable in the local script
end)

Local script:

--\\ Services
local RepStorage = game:GetService'ReplicatedStorage' -- Get the ReplicatedStorage service.
local Players = game:GetService'Players' -- Get players service.

--\\ Objects
local Remote = workspace:WaitForChild'Wait' -- Reference to the remote object.
local Gear = RepStorage:WaitForChild'GravityCoil' -- Reference to the gravity coil object.

--\\ GUIs
local Button = script.Parent
local InputGui = Button.Parent:WaitForChild'Input'

--\\ Client
local Client = Players.LocalPlayer
local Backpack = Client:WaitForChild'Backpack'

--\\ Conditions
local Status = false

--\\ Client event set-up
--\\ As a side note, you probably won't need to use a second argument for this event in this scenario.
Remote.OnClientEvent:connect(function(Request, ...) -- I always have 'Request' as a first argument, to make my code conditional.
	--\\ Request variable (not required, but it makes things easier)
	Request = tostring(Request):lower() -- Make it so the 'request' string isn't case sensitive.
	
	--\\ Make code run upon Request condition
	if(Request == 'test')then -- If I make the 'Request' argument 'test', then...
		Status = not Status -- Toggle the status bool
	end
end)

--\\ Using this event outside of the remote event.
script.Parent.MouseButton1Click:connect(function()
	if (InputGui.Text == 'Alpha!') and (Status == false) then
		print'Fired'
		Gear:Clone().Parent = Backpack
		InputGui.Text = ''
		Button.Text = 'Success!'
		wait(1)
		Button.Text = 'Submit'
	else
		InputGui.Text = 'Submit'
		Button.Text = 'Invalid Code'
		wait(1)
		Button.Text = 'Submit'
	end
end)

NOTE:

This is assuming everything in your game environment is as presented by the program. Meaning objects are in the right place, nothing is missing, and all the names are the same.

4 comments

0

Keeps saying "On server event can only be used on a server

0

Still doesnt help alot, but helps somewhat

0

Well what question are you trying to get answered

0

tyyyyy

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