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"
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)