I’m a beginner,
I’m trying to make a script that detects when the player clicks, I have no idea how I could make it, any help?
Do you want the player to click a GUI button or do you need them to click a part? Can you clarify more on what you are trying to achieve? There are multiple ways for multiple things.
Not any of those, I want to know how I can detect a player clicking in the screen.
Like Button.MouseButton1Click?
Yeah, but on the screen, I’ve been trying for a while now.
Yeah, I’m trying to make it on all of the screen.
make A GUI button and make it invisible and make sure it fits the screen and any time clicked it will tell you its the easy way
It’s supposed to turn on particles, though.
.MouseButton1Click:Connect(func)
is what I think you are looking for.
Use user input Services to make that happen and yeah
Hm, I’ll try that, Would it stop the sparkles after the player clicked?
Actually, I’ll try to experiment.
This Will Stop it after clicked yeah
Hm, doesn’t seem to start the particles, And it doesn’t have any errors.
Mind Showing the Script? I can help and see
If you’re doing through this a gui object (textbutton
), the player who clicked it would always be the local player. You can use a for loop to iterate through workspace, and enable particles on and off.
script.Parent.MouseButton1Click:Connect(function() -- Textbutton
local player = game.Players.LocalPlayer
print(player)
for i,v in pairs(game.Workspace:GetDescendants()) do -- Loop through workspace, you can if you want to have your particles in a folder, and loop through that instead
if v:IsA("ParticleEmitter") then
v.Enabled = true
wait(3)
v.Enabled = false
end
end
end)
Like @austingamer12345 mentioned, UserInputService would be a good way to accomplish this. Could you show us what you have currently?
Firstly, UserInputService does not work in “regular” server-side Scripts. Thus, in order to use this, you’ll need to have a Localscript locally stored, let’s say in StarterPlayerScripts which is a good area to place it. Then you’ll need to have a RemoteEvent stored in ReplicatedStorage (so that both the Script and Localscript can access it). And finally, have your server-side Script placed in Workspace or ServerScriptService (on the server).
Once you have that done, you’ll want to do something like this.
In Your Localscript:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
-- Variables
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseButton1 then -- When left mouse button is clicked
RemoteEvent:FireServer("Start") -- "Start" indicates it was pressed
end)
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseButton1 then -- When left mouse button is released
RemoteEvent:FireServer("End") -- "End" indicates it was released
end
end
In Your Script:
-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Variables
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
RemoteEvent.OnServerEvent:Connect(function(player, message)
if message == "Start" then
-- Start your particles
elseif message == "End" then
-- End your particles
end
end)
Hope this helps! And let me know if there’s any questions or errors with this.
I’ve used a very simple script, you’ll probably laugh at it, but here it is
local UserInputService = game:GetService(“UserInputService”
local function onInputBegan(input, gameProcessed)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
script.Parent.Enabled = true
end
Nevermind, I didn’t fix it, I made it a local script.
If I were you I would use @MJTFreeTime script, he did a good job explaining a good solution. But it all comes down to if you are trying to run this on Server or on the Client?
for your case i suggest checking PlayerMouse API reference: https://developer.roblox.com/en-us/api-reference/class/Mouse
especially this part: https://developer.roblox.com/en-us/api-reference/event/Mouse/Button1Down
you don’t need to make any screen guis for this to work
inside a local script do:
local Players = game:GetService("Players") --i suggest using :GetService for services
local Player = Players.LocalPlayer --get the player
local Mouse = Player:GetMouse() --get the mouse
Mouse.Button1Down:Connect(function()
--run code here
print("mouse was clicked at pos x:" .. Mouse.X .. " y:" .. Mouse.Y) --thats just to test it
end)