I've been working on a script to pickup and drop items, I used a local script to get what button a player presses, but unfortunately I can't do things such as cloning from a local script, I want to be able to fire a script when the local script gets a button press, from what I've researched there may be a way to do this with bindable events? I don't understand any of it though. How would I go about doing this?
You can use remote events:
Create a remote event in the replicated storage and name it cloneItem
In a local script:
01 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 |
03 | --keypress event that runs when a player presses a button: |
04 | game:GetService( "UserInputService" ).InputBegan:Connect( function (button, gameProcessed) |
05 | -- if the player is typing into chat, clicking the leaderboard, or is in the game settings, then don't process the input instead, exit out of this function. |
06 | if gameProcessed then return end |
07 |
08 | -- when the player presses f, I am telling the server that I want to run whatever code is connected to the cloneItem event on the server. |
09 |
10 | -- in this example I am passing a string containing the word car. |
11 |
12 | --by passing "Car" as a parameter I am telling my server scripts that I want to clone an item named car. |
13 | -- see the server script below for more information |
14 | if input.KeyCode = = Enum.KeyCode.F then |
15 | replicatedStorage.cloneItem:FireServer( "Car" ) |
16 | end |
17 | end ) |
ServerScript:
01 | local ServerStorage = game:GetService( "ServerStorage" ) |
02 |
03 | --this code runs when the player calls the cloneItem:FireServer() |
04 | --itemName is the name of the model that we want to clone into the game |
05 | --in the local script above I told the server that I wanted to spawn a car. |
06 |
07 | game:GetService( "ReplicatedStorage" ).cloneItem.OnServerEvent:Connect( function (playerWhoFiredEvent, itemName) |
08 | print (playerWhoFiredEvent, itemName) |
09 |
10 | --look for the item to clone in the replicated storage |
11 | local model = ServerStorage:FindFirstChild(itemName) |
12 |
13 | --if we found the model, clone it into the workspace: |
14 | if model then |
15 | local clone = model:Clone() |