Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I go about linking local scripts to normal ones?

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?

1 answer

✓ Accepted answer

You can use remote events:

Create a remote event in the replicated storage and name it cloneItem

In a local script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

--keypress event that runs when a player presses a button: 
game:GetService("UserInputService").InputBegan:Connect(function(button, gameProcessed)
        -- 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. 
        if gameProcessed then return end 

        -- 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. 

        -- in this example I am passing a string containing the word car. 

        --by passing "Car" as a parameter I am telling my server scripts that I want to clone an item named car. 
       -- see the server script below for more information
        if input.KeyCode == Enum.KeyCode.F then  
                replicatedStorage.cloneItem:FireServer("Car")
        end
end) 

ServerScript:

local ServerStorage = game:GetService("ServerStorage") 

--this code runs when the player calls the cloneItem:FireServer() 
--itemName is the name of the model that we want to clone into the game  
--in the local script above I told the server that I wanted to spawn a car. 

game:GetService("ReplicatedStorage").cloneItem.OnServerEvent:Connect(function(playerWhoFiredEvent, itemName) 
          print(playerWhoFiredEvent,  itemName) 

        --look for the item to clone in the replicated storage
         local model = ServerStorage:FindFirstChild(itemName)
 
        --if we found the model, clone it into the workspace:
        if model then 
               local clone = model:Clone()
               clone.Parent = workspace
        else 
                warn("Game couldn't clone " .. itemName .. " since it was not found in the server storage") 
        end

end)

6 comments

0

Lmk if you need any more explanation

0

I don't understand much of what is going on, but I assume I replace "Car" with the model/part I am trying to clone, however line 10 gives an error Expected identifier when parsing expression, got ')' Could you try to explain a bit more of what's going on?

0

I had an extra ) I'll edit that out my bad. We can talk it over on discord if you want? Lucy#0003

0

Otherwise here is fine it's up to you. Added some more clarification. 

0

This works! Thank you so much for your help, I actually feel as if I understand this and can replicate it on my own, after adding the position & rotation for the tool I can now pickup and drop items at the exact location of the tool. Thanks so much for your help!

0

np

This discussion is preserved as it was. New answers, comments, and votes are closed.