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

How can I use an event to clone an item to the player?

Asked by 7 years ago

Basically I have a customization system. When the player selects a hat, I need the hat to be cloned and put into the players character.

I'm not to familiar with using events, etc. How can I go about using an event or at least make this work in FilteringEnabled? I simply need to clone an item into the player's character in Workspace from ReplicatedStorage.

1 answer

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

Hey ViciousViperV2,

I will guide you as to how I would go about doing this with FilteringEnabled on. First, I would put a RemoteEvent in Replicated Storage and then put the LocalScript under the Button that the player will need to click in order for the hat to be selected. Then, when the player selects a hat, I would activate that RemoteEvent's OnServerEvent, which the ServerScript should be listening for and then the ServerScript is what would clone the hat into the Character. That's the basis of what I would do. I will show you a personal example of the 2 events in-use and link you to a wiki page that can help you perhaps understand them better. Here is the wiki page for RemoteEvents being used and below is the code that will show a RemoteEvent in-use.

MouseButton1Down Event in the LocalScript to fire the RemoteEvent:

local button = script.Parent -- Declares a variable for the button that will be pressed to select the hat.
local rs = game:GetService("ReplicatedStorage")  -- Declares a variable for Replicated Storage.

button.MouseButton1Down:Connect(function() -- Anonymous function using the MouseButton1Down event of a Button, which detects a left click down. Wasn't sure how you wanted the selection to work but, you can substitute this event for another.
    local event = rs:WaitForChild("RemoteEvent") -- Declares a variable for the Remote Event.
    event:FireServer() -- Activates the OnServerEvent for the Remote Event's listeners
end) -- end for the anonymous function. The parenthesis is important because that's how anonymous functions end.

OnServerEvent in the ServerScript to make all the cloning happen:


local rs = game:GetService("ReplicatedStorage") -- Declares a variable for the ReplicatedStorage local hat = rs:WaitForChild("Hat") -- Declares a variable for the Hat that is being cloned. local event = rs:WaitForChild("RemoteEvent") -- Declares a variable for the RemoteEvent event.OnServerEvent:Connect(function(plr) -- Anonymous function listening to the OnServerEvent from the RemoteEvent. local char = workspace[plr.Name] -- A variable for the Character. local new_hat = hat:Clone() -- A variable for the hat's clone. new_hat.Parent = char -- Parenting the hat to the Character. end) -- end for the anonymous function.

By the way, I chose to parent the Remote Event and Hat to ReplicatedStorage because, in FilteringEnabled, ReplicatedStorage is available to both the Client and the Server.

Well, I hope I helped out in one way or another. Below are all the links.

Anonymous Functions

Remote Events API Page

:GetService() Method

:WaitForChild() Method

MouseButton1Down Event

OnServerEvent

:FireServer() Method

~~ KingLoneCat

0
Wow, thanks for the quick and detailed response. However, here is my issue. I have 100's of assets in ReplicatedStorage, and using i,v in pairs to get all of the items, and generate the button. When that button is pressed, I need to send the item as like an object, to then find in RepStorage and clone. How would I do that? ViciousViperV2 24 — 7y
0
I would find the name of the item that the button is pressed for. I don't know how your game is made so I'm not sure how you would get the name of the item that is selected from the customization system but, when you do that, use for _,v in next, ReplicatedStorage:GetChildren() do and then, check if v is equal to the object that is selected's name. You can send the name of the object as an argumen KingLoneCat 2642 — 7y
0
As an argument, like this: :FireServer([name of the object]) and then to receive that, all you have to do is in the function that receives it add another parameter for the argument so, like this: event.OnServerEvent:Connect(function(player, name) for _,v in next, rs:GetChildren() do if v.Name == name then [CODE] end end end) KingLoneCat 2642 — 7y
Ad

Answer this question