Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Question On Bindable Event?

Asked by 6 years ago

Hi guys, I've written a code that works offline but doesn't work online. What I wanted to achieve is when i pressed on the clickdetector located inside my "Part", I can print "ok2" located inside StarterPack.Can anyone tell me whats wrong?:

Script inside a "Part"

btn = script.parent
clk = btn:WaitForChild("ClickDetector")
callplayerevent = game:GetService("ReplicatedStorage"):WaitForChild("CallPlayerEvent")

clk.MouseClick:connect(function(plrr)
    callplayerevent:Fire(plrr)
end)

LocalScript inside "StarterPack"

player = script.Parent.Parent --Yes, I know there's a simpler way
callplayerevent = game:GetService("ReplicatedStorage"):WaitForChild("CallPlayerEvent")

callplayerevent.Event:connect(function(plr)
    print("ok2")
end)

Objects I've used: 1) BindableEvents named "CallPlayerEvent" located in replicatedstorage 2) Part Named "SignIn" 2) Click Detector named "ClickDetector" located inside "SignIn"

1 answer

Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago
Edited 6 years ago

Bindable Events do not work server to client. It can only be used for server to server communication, or clent to client communication if the client (player) is the same player.

That being said, there is a way to communicate server to client using Remote Events and Functions. In your case, since no value needs to be returned, you would use a remote event. With remote events and functions, you can fire the event to a specific player, or, using Remote Events, you can fire it to all players, but in this case, we are firing one client.

Make a RemoteEvent in the ReplicatedStorage, and name it CallPlayerEvent

In the Script

local btn = script.Parent
local clk = btn:WaitForChild("ClickDetector")
local callplayerevent = game:GetService("ReplicatedStorage"):WaitForChild("CallPlayerEvent")

clk.MouseClick:Connect(function(player)
    callplayerevent:FireClient(player)
end)

In the LocalScript

local player = game.Players.LocalPlayer -- Use this way.
callplayerevent = game:GetService("ReplicatedStorage"):WaitForChild("CallPlayerEvent")

callplayerevent.OnClientEvent:Connect(function()
    print("ok2")
end)
0
Actually, I was trying to establish a communication between a "Part" in the Workspace with the Player. The script was inside "Part", so it's not a serverscript. I was trying to do this because "Part" cannot use FireServer(), as it prompts only the client could do it, resulting in me trying to do it like this: Player Clicked "Part">"Part" tells whichever player clicked on it to FireServer(). lesliesoon 86 — 6y
0
The function used in the scripts above is "FireClient", not "FireServer". FireClient is a server function that calls OnClientEvent for the player. Also, you can simply use "FireServer" in the local script if the objective is to tell the client to fire the server. UgOsMiLy 1074 — 6y
0
Wow thanks so much! lesliesoon 86 — 6y
Ad

Answer this question