Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you make a value "update" in a RemoteEvent?

I'm working on a shop that uses a remote event. But I can't seem to figure out how to make the value change to what it needs to be instead of being the one that was set in studio.

-- References
local GameItems = ReplicatedStorage.GameItems

--------------------------------------------------------------------

ReplicatedStorage.RemoteEventBuy.OnServerEvent:Connect(function(plr, Selected, SelectedPrice)
    local leaderstats = plr:WaitForChild("leaderstats")
    local Gold = leaderstats:WaitForChild("Gold")
   
    local Backpack = plr:WaitForChild("Backpack")
    local StarterGear = plr:WaitForChild("StarterGear")
    print(Gold.Value)
    print(SelectedPrice.Value) <-- This prints out 0 because thats what it is set to in studio.
    print(Selected.Value)  <-- This prints blank because thats what it is set to in studio.
    
    if Gold.Value >= SelectedPrice.Value then
        Gold.Value = Gold.Value - SelectedPrice.Value
        local NewItemBackpack = GameItems[Selected.Value]:Clone()
        local NewItemStarterGear = GameItems[Selected.Value]:Clone()
        NewItemBackpack.Parent = Backpack
        NewItemStarterGear.Parent = StarterGear
        
        print("Succesfully sold!")
    else
        warn("This wont work")
    end
end)

1 answer

✓ Accepted answer

You should send the values over the remote event instead of value objects like you're doing right now. For example on the client, you could store data on items in a table and pass it through the remote event.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- This data could be displayed on the client (for example, a shop menu)
local items = {
	item1Name = {
		price = 100;
	};
	-- etc.
}

-- an outline example of interacting with the user interface
button.Activated:Connect(function()
	local item = ... -- get item associated with this button
	local price = items[item].price
	ReplicatedStorage.RemoteEventBuy:FireServer(item, price)
end)

On the server, you would keep a separate copy of the data (in a location hidden from the client such as ServerStorage). You would use this to make sure that a valid request was sent from the server, and then you would grant them the item.

ReplicatedStorage.RemoteEventBuy.OnServerEvent:Connect(function(plr, Selected, SelectedPrice)
	if items[Selected].price ~= SelectedPrice then return end
	-- give them the item
end)

1 comment

0

I forgot to mention on how the selected value is set. You set it by pressing on the option that you want and then you press buy (My bad!) . But I'll try figuring it out on how you said it here.

This discussion is preserved as it was. New answers, comments, and votes are closed.