Hello,
The script is in a tool and I would like send event to a local script in the GUI of the player who have the tool when the tool is activated. Can you help me please?
Remote Events work cross-server snd client. If you need an event just on the client, use a bindable event which only the client will see. If you are using a tool, Instead of using a bindable event you can detect when it is equipped using a event from the tool.
Tool.Equipped:Connect(function()
--do Something
end)
If the “script” is in the Tool, you can use (As Wizard said) RemoteEvents to fire from the server to the client so let’s say we put this inside the script:
--This is just a broad example
local Character = script.Parent.Parent.Parent
local Event = game.ReplicatedStorage:WaitForChild("Event")
Tool.Activated:Connect(function()
Event:FireClient(Character)
end)
We can use what’s called a FireClient()
function so that when the tool is activated, it will fire from the server to the client (You’ll need the function to pass through a player object on the first argument however, I haven’t tested this myself). Then on the local script you can define your Event, put your line of codes and whatever here:
local Event = game.ReplicatedStorage:WaitForChild("Event")
Event.OnClientEvent:Connect(function(Player)
--Insert random things here
end)
Can I write game.ReplicatedStorage.Event
instead game.ReplicatedStorage:WaitForChild("Event")
?
You don’t neccessarily have to use a RemoteEvent to capture this. You could just use a LocalScript attached to the Tool:
local Tool = script.Parent
function onActivated()
print("Tool Activated")
-- Do GUI stuff
end
Tool.Activated:connect(onActivated)
You could, but the WaitForChild()
function is just checking to see if the Event is there, otherwise it returns back as an infinite loop if it isn’t in the desired spot
You can do that but the client might not see it right away causing errors. Using WaitForChild()
is definitely safer.
Whenever you are defining something that needs to load, you always want to use :WaitForChild()