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

Needing Help Enabling Particles in Character?

Asked by 4 years ago

i am new to scripting and this is probably a really easy thing to do but i am trying to make this script enable some particles that i have inside of players character but i got it working but when you enable it only the player can see it and not everyone i would appreciate some help if anyone knows this is the script

script.Parent.MouseButton1Click:Connect(function()
    if game.Players.LocalPlayer.Character.Chest.Liquid.Particles.Enabled == false then
        game.Players.LocalPlayer.Character.Chest.Liquid.Particles.Enabled = true
    else
        game.Players.LocalPlayer.Character.Chest.Liquid.Particles.Enabled = false
    end

end)

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Alright. It's a fine piece of code you have right there, but unfortenately we have to make it way more complicated. I hereby introduce to you: RemoteEvents!

Wiki page: https://developer.roblox.com/en-us/api-reference/class/RemoteEvent

Basically, when you are playing roblox, you are a client playing on a server. The client is your computer, and the server is something roblox is running on their own.

There are two types of scripts that you have to know about when trying to communicate with the server. Serverscripts and Localscripts. Serverscripts are named "Script" in roblox studio

The code you wrote was written in a localscript. Localscripts only change information on the client, in other words, on your computer (Locally. it is in it's name). It does not replicate over to the server. That means only you can see what changes the localscript does.

We need the client to communicate with the server so the server can make the change, which will give everyone the ability to see what is happening! We will use RemoteEvents for this.


Step 1: You have to put a RemoteEvent in "ReplicatedStorage". This is a "Vault" that both the client and the server can see. This is our way of communicating with the server. I will name it "enableParticles"

http://gyazo.com/be72eb22d9281c0c8cc47c3b86500cfe


Step 2: Then we get to the part of setting up a localscript: First I will listen to the MouseButton1Click event to detect whenever the player clicks the button. Then we have to locate the "enableParticles" event and define it with a variable. NOTE: I do not change the particles state on the client. I will do that in a serverscript later.

It is important that you keep your scripts organized with variables. And please do use game:GetService() for services that you need.

local ReplicatedStorage = game:GetService("ReplicatedStorage") --This is exactly the same as game.ReplicatedStorage, but you can see the difference in this article: https://devforum.roblox.com/t/use-of-getservice/204362
local particleEvent = ReplicatedStorage:WaitForChild("enableParticles")



script.Parent.MouseButton1Click:Connect(function()
    particleEvent:FireServer() --This is probably something new for you. Here I am "Getting contact" with the server. It's like that i have a button, and when I press it, the server will notice me pressing that button and do things I have set it to do when I press that button.
end)

That's the localscript. Now that we have the "Button" that the client can press, we have to make a script that fires every time that "button" is pressed.

I placed a ServerScript inside "ServerScriptService". This is a good location for you to store serverscripts.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local particleEvent = ReplicatedStorage:WaitForChild("enableParticles")

--Here comes the complicated stuff:


particleEvent.OnServerEvent:Connect(function(player) --This right here is the part that is waiting for the "Button" to be pressed. Whenever the button is pressed, the code inside this function will be executed. The first argument for "OnServerEvent" will always be the player who "Pressed" the button.

    local character = player.Character --Defining character since we know the player
    local particle = character.Chest.Liquid.Particles --Defining where the particle is located
    particle.Enabled = not particle.Enabled --Here we are setting the state of the particle not equal to what it currently is. This means that if the particle.Enabled is true, set the particle.Enabled to anything else than true. The only other option is false, which means the particle will disable. And it's the same if particles.Enabled is false.
end)

I hope you understood something of the cluster I just launched at you

If you got questions, please contact me at my discord: Sethex#6760

Good luck with your scripting!

~Sethex

Ad
Log in to vote
0
Answered by 4 years ago

Well, considering you are using "LocalPlayer" I'm assuming you put this in a LocalScript. Instead put this in a script or use a Remote Function. A LocalScript is a script where only one player can see what is happening when they fire an event. A Remote Function may be advanced for you if you are new to scripting, but it allows the client to communicate to the server then back to all clients in the server. If any of this stuff confuses you I'll leave some links down below for you to read up on.

What is a local script? https://developer.roblox.com/en-us/api-reference/class/LocalScript

What are remote functions and remote events? https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

Feel free to ask anymore questions :)

Answer this question