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

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

Asked by
JPT79 92
4 years ago

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

Log in to vote
2
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)
0
Lmk if you need any more explanation royaltoe 5144 — 4y
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? JPT79 92 — 4y
0
I had an extra ) I'll edit that out my bad. We can talk it over on discord if you want? Lucy#0003 royaltoe 5144 — 4y
0
Otherwise here is fine it's up to you. Added some more clarification.  royaltoe 5144 — 4y
View all comments (2 more)
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! JPT79 92 — 4y
0
np royaltoe 5144 — 4y
Ad

Answer this question