(Roblox) How to string a RemoteEvent?

Viewed 32

I use this script:

local milk = game.ReplicatedStorage.Milk
local price = 10

local converterNum = tonumber(milk.Value)
--local converterString = tostring(price)


print(tonumber(milk.Value))

script.Parent.MouseButton1Click:Connect(function()
    if converterNum >= price then
        converterNum = converterNum - price
        price = price + 10
    else
        print("error")
    end
end)

And the error is

Value is not a valid member of RemoteEvent "ReplicatedStorage.Milk" 

enter image description here

I dont understund how i can string it i try with tonumber or tostring but it wont work, Please Any Suggestion I need this really Thanks

1 Answers

Since milk is a remote event, it doesn't have a "value" property

For this to work, you'd need 2 scripts

Put one in ServerScriptService with the contents below

-- First, you actually need your leaderstats

game.Players.PlayerAdded:Connect(function(plr)
   local leaderstats = Instance.new("Folder", plr)
   leaderstats.Name = "leaderstats"
   local coins = Instance.new("IntValue", leaderstats)
   coins.Name = "Coins" -- You can change this
end)
-- Check if someone clicks the button
game.ReplicatedStorage.Milk.OnServerEvent:Connect(function(Player, Price)
    Player.leaderstats.Coins.Value -= Price
end)

And one in your UI

script.Parent.MouseButton1Click:Connect(function()
    if game.Players.LocalPlayer.leaderstats.Coins.Value >= price then
        game.ReplicatedStorage.Milk:FireServer(price)
        price += 10
    else
        print("Not enough cash")
    end
end)

Whenever you click your button, if you have 10 coins, you should lose 10 coins, then have to pay 20 coins next time!

Related