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

Random tool script not working? Other players can't see it?

Asked by 5 years ago

I'm using a script that gives the player a random tool from a set amount of tools that are all located in a folder in RepicatedStorage, but when the script gives the tool the randomizing works but no other player can see the tool, the script is a local and people have told me it has to use remote event but I saw something called remote function, now I'm confused on which one to use, and how to use it since I'm just learning about these two things work here's the script if it helps you find out how these would work with remote event or function :)

Weapons = game.ReplicatedStorage.Weapons:GetChildren()
local Weapon = Weapons[math.random(1,#Weapons)]:Clone()
Weapon.Parent = game.Players.LocalPlayer.Backpack

1 answer

Log in to vote
0
Answered by 5 years ago

For that you would use a RemoteEvent. According to Wiki, "The first difference is that RemoteFunctions are designed for two way communication, while RemoteEvents are designed for one way. A RemoteFunction sends a request and then waits for a response, while RemoteEvents just send a request." With that in mind, here is a little example on how to use a RemoteEvent and RemoteFunction.

RemoteEvent

-- RemoteEvent from the server
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createExplosion = Instance.new("RemoteEvent")
-- just creating the remote from scratch
createExplosion.Name = "CreateExplosion"
createExplosion.Parent = ReplicatedStorage

createExplosion.OnServerEvent:Connect(function(player, otherPlayer)
    -- otherPlayer is a parameter which the client will send to the server! but be careful, as clients can send anything. 

    if Players:FindFirstChild(otherPlayer) then -- A client can send anything, to be safe let's check if it's a valid name!
        local plr = Players[otherPlayer]
        local exp = Instance.new("Explosion") -- creates explosion 
        exp.Position = plr.Character.Head.Position -- positioning in head so it kills
        exp.Paarent = workspace -- parent to workspace so it explodes
    end
end)

From the client:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local createExplosion = ReplicatedStorage:WaitForChild("CreateExplosion")

createExplosion:FireServer("chad_universal") 
-- fires remote, hehehe i exploded you

RemoteFunction

Now a remote function. They do not have any event to listen for, you instead assign it a callback. An example for using a remote function would be to check the price for a house, then the client could request the price. Remote functions must return, so that the client could get what the server returns to it, or vice versa if you're doing server to client. If you don't need to return anything use an event instead.

-- RemoteFunction from the server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local checkPrice = Instance.new("RemoteFunction")
checkPrice.Name = "CheckPrice"
checkPrice.Parent = ReplicatedStorage

local function getPrice(player) -- first parameter is also the player
    local price = workspace.House.Price

    if player.leaderstats.Cash.Value >= price.Value then 
        -- Buying house logic here 
    end

    return workspace.House.Price.Value -- return house price back to client 
end

checkPrice.OnServerInvoke = getPrice -- Assigns OnServerInvoke to our getPrice. Remember to exclude the brackets! Not excluding them will assign OnServerInvoke to what it returns, that being our price value. 

From the client:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local checkPrice = ReplicatedStorage:WaitForChild("CheckPrice")

local price = checkPrice:InvokeServer() -- This is actually the price of the house, remember we returned the value? 

print(price) --> will print the house price, mine was 3000 so it printed 3000! 
Ad

Answer this question