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
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 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)
local ReplicatedStorage = game:GetService("ReplicatedStorage") local createExplosion = ReplicatedStorage:WaitForChild("CreateExplosion") createExplosion:FireServer("chad_universal") -- fires remote, hehehe i exploded you
-- 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.
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!