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

Why does a certain thing work when in Studio but not when published to Roblox?

I want to say two things before you say anything:

#1: I am NOT using local scripts

#2: I am using remoteEvents to communicate between scripts.

For example, I have a string value 'slot' that only one player can have. I also have a regular script that clears the slot once a certain remoteEvent is triggered. Other normal scripts will fire that remoteEvent if certain things have been fulfilled, such as no health on humanoid, or a specific button has been pressed. Also, upon testing the game, I opened the dev console and it says 'FireServer can only be called from client'. I tried changing the script inside the button to FireServer into a local script but still no results.

local repStorage = game:GetService("ReplicatedStorage")
local exitQueue = repStorage:WaitForChild("exitQueue")
local gui = script.Parent

gui.MouseButton1Click:Connect(function()
	exitQueue:FireServer()
end)

If a player presses on the GUI button the remoteEvent will be triggered. Note that the GUI is a SurfaceGui, like on the surface of a brick.

local repStorage = game:GetService("ReplicatedStorage")
local exitQueue = repStorage:WaitForChild("exitQueue")
exitQueue.OnServerEvent:Connect(function()
	--do something
end)

And the code above is the receiving code. It does not work when on a server but works on the client, in studio.

2 comments

0

We need some code m8

0

It's not a matter of, don't use local scripts, but how the code is set up. Clients are only able to access some features so you use RemoteEvents to communicate to the server to do very important things. Provide code if you want further help.

1 answer

✓ Accepted answer

###Remotes are dependent on a client firing them and the server listening for them, or vice versa, server firing them and client listening for them. You can't use all server script or all local scripts. There is an alternative to Remotes, those being Bindables. Bindables are not for client-server communication, they are for client-client communication or server-server communication.

###Here is an example of a BindableEvent:

local bindable = Instance.new("BindableEvent")
bindable.Name = "ExampleEvent"
bindable.Parent = workspace

bindable.Event:Connect(function(position) -- we can add parameters! 
    local part = Instance.new("Part")
    part.Position = position 
    part.Parent = workspace
end) 

bindable:Fire(Vector3.new(0, 50, 0)) 
-- create our part! arguments go in between the brackets

###Here is an example of a BindableFunction:

local bindable = Instance.new("BindableFunction")
bindable.Name = "ExampleFunction"
bindable.Parent = workspace

local function example(gun)
    -- maybe you want to get the price of a gun
    return game:GetService("ServerStorage")[gun].Price.Value -- a numbervalue named price
    -- remote/bindable functions are to return 
end

bindable.OnInvoke = example -- OnInvoke is a callback and expects a function to be assigned to it 

local price = bindable:Invoke("M4") -- price of gun 
print(price) --> 375 
-- just a random price i put

1 comment

0

Thank you! I will look up for more tutorials on Bindable Events/Functions to make sure I get everything right.

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