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

Correction on using RemoteEvents or RemoteFunctions?

Asked by 4 years ago
Edited 4 years ago

So, this is quite a difficult problem to try and explain but I will try my best, aha.

What I want my script to do is to loop through a model, and find out which TextButton was clicked and then turn that TextButton.Name into a variable. (This is all done through a local script as it is doing work with GUI's). I then want it to send the variable name through a RemoteEvent/RemoteFunction (not entirely sure on which one to use) and for it to take the variable name and then to clone a tool with the same name and put it into the player's backpack for all players to see. This is the code that I was previously using before I found out I even had to use RemoteEvents/functions, (stupid I know), but now I've found out it is needed to be changed.

Code:

local frame = script.Parent.Frame
local tool = game.ReplicatedStorage.Crisps

for i,v in pairs(frame:GetChildren()) do
    game.Workspace.Bowl.ClickDetector.MouseClick:Connect(function(player)
    frame:TweenPosition(UDim2.new(0.125, 0,0.141, 0))
    v.MouseButton1Click:Connect(function()
    local flavour = v
        for i,v in pairs(tool:GetChildren()) do
            if v.Name == flavour.Name then
                local added = script.Parent.added
                v:Clone().Parent = player.Backpack
                added.Text = "Added one packet of "..v.Name
                added:TweenPosition(UDim2.new(0.03, 0,0.032, 0))
                wait(2)
                added:TweenPosition(UDim2.new(0.03, 0,-0.2, 0))
                frame:TweenPosition(UDim2.new(0.113, 0,1.05, 0))
            end
        end     
    end)
    end)
end

I hope you understand what problem I was trying to solve.I also don't want corrections on my code, I want to actually gain something from the advice so I can use it for the future as there is clearly a problem in my learning, please don't change my code around if it is not needed just now, I just want it to function correctly.

1 answer

Log in to vote
1
Answered by 4 years ago

The difference between RemoteEvent and RemoteFunction is that RemoteEvent just sends some stuff (or even nothing) and that's it.

RemoteFunctions sends some stuff (like tables, numbers, strings etc same as RE) and then waits for a response (a "return", just like in a normal function, using "return" there sends the player what you wrote after return). To clarify: this one waits. the script will not keep going, it will wait until the server sends something back. (at least the thread you have running rn)

You need to make a RemoteEvent (because you don't need a response, you just need to let the server know you want this tool and then do nothing else), mostly made in ReplicatedStorage. And connect a function to it on the server (the one that takes the flavor and then clones it and puts it in the player's backpack)

Now what you want to do is loop through all the buttons in the model, connect a function to MouseButton1Click and then "fire" the event you made in ReplicatedStorage.

so it would be something like this:

-- in a script
local plrRequestsFlavor = Instance.new("RemoteEvent",game.ReplicatedStorage)
plrRequestsFlavor.Name = "RequestFlavor"

plrRequestsFlavor.OnServerEvent:Connect(function(plr, flavor) -- when an RE or RF is fired the very first parameter is the player who fired it and everything else is everything else you sent while "firing" the event.
    -- you put the code for cloning and parenting it to plr.Backpack
end) 
-- in a localscript
local RequestFlavor = game.ReplicatedStorage:WaitForChild("RequestFlavor")

function mouseClicked(TextButton) -- connect this to every button's MouseButton1Click and send the button as the first argument.
    local flavor = TextButton.Name
    RequestFlavor:FireServer(flavor) -- now this flavor var is gonna be "flavor" on the server script. btw you don't send in the LocalPlayer, roblox does this for you.
end

I hope this helped :D

Ad

Answer this question