Ad
Log in to vote
0

How can I turn this local script to a server script?

Asked by 14 days ago

Now before you tell on me how easy It Is, let me explain, anyway I wanted this script to be a server script, so that whenever the button Is clicked, It gives you a weapon, and It does work however Its a local script, the problem is that since local scripts are for the client side, the weapons that are given to you suddenly lose there function (And If you view the server side, you will be holding nothing)

script:

local button = script.Parent
local ScrollingFrame = script.Parent.Parent.Parent
local Rep = game.ReplicatedStorage
local Knife = Rep.Weapons["Average knife"]
local Gun = Rep.Weapons.Pistol

button.MouseButton1Click:Connect(function()
    ScrollingFrame.Visible = false
    local P = game.Players.LocalPlayer
    local CopiedGun = Gun:Clone()
    local CopiedKnife = Knife:Clone()
    CopiedKnife.Parent = P.Backpack
    CopiedGun.Parent = P.Backpack
end)
0
you cant ask for player input from a server script VAnkata20 49 — 14d
0
About your request @imnotaguest1121 You can use getdescendants/getchildren on the "Weapons" folder and if statements. If you don't know how you can make a question about it. hattim00 75 — 14d

1 answer

Log in to vote
1
Answered by 14 days ago
Edited 14 days ago

[MARK THIS ANSWER AS ACCEPTED IF IT WORKS]

For the client to communicate with the server, you have to use something known as RemoteEvents. In order for a client to send a message to the server, it needs to fire the RemoteEvent with the function RemoteEvent:FireServer in a LocalScript.

The server meanwhile needs to connect a function to the OnServerEvent of the RemoteEvent.

When a client calls the FireServer function, any functions on the server that are connected to OnServerEvent will fire. Note that this is not immediate - the network connection between the client and server will determine how quickly this happens.

When firing a RemoteEvent from a client to the server, data can be included in the firing. By default, the functions connected to OnServerEvent will be passed the player who fired the event as the first parameter. If any other arguments are provided in the FireServer function, they will also be included in OnServerEvent after the player argument.

For your instance create a RemoteEvent in ReplicatedStorage and a script in ServerScriptService.

Configure the local script to this:

local button = script.Parent
local ScrollingFrame = script.Parent.Parent.Parent
local Rep = game.ReplicatedStorage
local Knife = Rep.Weapons["Average knife"]
local Gun = Rep.Weapons.Pistol

button.MouseButton1Click:Connect(function()
    ScrollingFrame.Visible = false

    Rep.RemoteEvent:FireServer(game.Players.LocalPlayer)
end)

The script in ServerScriptService should be:

local Rep = game:GetService("ReplicatedStorage")
local event = Rep.RemoteEvent
local Knife = Rep.Weapons["Average knife"]
local Gun = Rep.Weapons.Pistol

local function giveTools(player)
    local CopiedGun = Gun:Clone()
    local CopiedKnife = Knife:Clone()
    CopiedKnife.Parent = player.Backpack
    CopiedGun.Parent = player.Backpack
end

event.OnServerEvent:Connect(giveTools)

Your explorer should look something like this: https://prnt.sc/DwPgTGm8Gopd

0
Thanks! It works! but I was wondering Instead of copy and paste the local scripts, server scripts, and remoteevents for each tool giver GUI, a script rounds up all weapons Inside a folder called "Weapons" Inside ReplicatedStorage, then whenever a player clicks on like a sword giver GUI, the local script opens fires the remoteevent, then the remoteevent triggers a script that opens the folder imnotaguest1121 74 — 14d
0
Then the script tries to find a weapon named sword (Or whatever I like to name It such as "Weapon" or "Kings sword") Then giving It to you (Also I wan't the script to affect GUI tools givers that gives more than 1 tool) imnotaguest1121 74 — 14d
0
You can use getdescendants/getchildren on the "Weapons" folder and if statements. If you don't know how you can make a question about it. hattim00 75 — 14d
0
Sure Ill do imnotaguest1121 74 — 14d
Ad

Answer this question