I have a function in a server script that I need to activate when a player clicks on an object. I got it to work using ray casting and remote events, but it eventually caused problems and conflicts with other code. I had a local script detect if it clicked the object that needs to be clicked, then fire a remote event which in turn fired that function I mentioned in the beginning. It got messy, so I am looking for an alternative. Any ideas?
Edit: after a little more research it seems as if a bindable event is what I am looking for. Still open to other ideas though while I try this out.
I am trying to make as little happen on the local side as possible. If this isn’t the proper way to do it then it might be easier. I am just trying to do all the code and checks on the server.
This was one of the first things that I tried. The click detector is in an object that is not permanent, and there could be many of them or none at all. My issue was that I couldn’t make the function in another server script to run from this. Is there a similar thing to remote events that I can fire from the server so another script can fire from that?
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local model = --the model to click
mouse.Button1Down:Connect(function()
if mouse.Target and mouse.Target:IsDescendantOf(model) then
RemoteEvent:FireServer()
end
end)
local button = --Specify it here (ex: script.Parent.Button)
local remote = game:GetService("ReplicatedStorage"):WaitForChild("Specify the remotes name here")
button.MouseButton1Click:Connect(function()
remote:FireServer()
end
If a button on the server, inside a LocalScript.
local Click = instance.new("ClickDetector", script.Parent) --script.Parent is the Parent of the thing you want clicked
local event = game:GetService("ReplicatedStorage"):WaitForChild("Specify the remotes name here")
Clickdetector.MouseClick:Connect(function()
event:FireServer() --It has to be a server remote. If its a client remote, go to the next one
end)
If a button on the server, inside a ServerScript. This is required inside a ServerScript.
local Click = instance.new("ClickDetector", script.Parent) --script.Parent is the Parent of the thing you want clicked
local event = game:GetService("ReplicatedStorage"):WaitForChild("Specify the remotes name here")
Clickdetector.MouseClick:Connect(function(plr)
event:FireClient(plr) --It has to be a client remote. If its a serverremote, see the script above.
end)
I just don’t know what is clicked. Let me know if you need additional help.