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

Save Button script doesn't seem to work in-game?

Asked by 5 years ago

In my studio, this save button script works completely fine with the Remote Function.

Client Sided:

local Player = game.Players.LocalPlayer
local MoneyStat = Player.Data.Money

local Id = Player.UserId
local SaveFunction = script.Script.save

local SaveButton = script.Parent
local CanSave = true

local Sound = script.Script.select
local Cooldown = 5

SaveButton.MouseEnter:Connect(function()
    SaveButton.TextColor3 = Color3.new(255, 255, 0)
end)

SaveButton.MouseLeave:Connect(function()
    SaveButton.TextColor3 = Color3.new(255, 255, 255)
end)

ValueDecrease = function(Value, GuiObject)
   Sound:Play()
   GuiObject.Text = Value

        repeat wait(1)
        Value = Value - 1

        GuiObject.Text = Value
        until Value == 0

        GuiObject.Text = "Save"
end


SaveButton.MouseButton1Down:Connect(function()
    if CanSave then
    CanSave = false

    SaveFunction:InvokeServer()

    ValueDecrease(60, SaveButton)

        CanSave = true
    end
end)

Server Sided:

local SaveFunction = script.save

SaveFunction.OnServerInvoke = function(Player)
    local Data = Player.Data
    local Id = Player.UserId

    local Stat = Data.Money
    local DataStoreService = game:GetService("DataStoreService")

    local Money = DataStoreService:GetDataStore("Money")

    Money:SetAsync(Id, Stat.Value)
end

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

This is because RemoteFunctions need to return, and since there's nothing to return, just use a RemoteEvent instead. RemoteFunctions are used for things like checking the price of a car in game, and a RemoteEvent would be used in spawning a car. Also, you have your Script and RemoteFunction inside the PlayerGui, which the script will not run. Place the RemoteEvent in ReplicatedStorage and the Script in ServerScriptService.

local Player = game.Players.LocalPlayer
local MoneyStat = Player.Data.Money

local Id = Player.UserId
local SaveEvent = game.ReplicatedStorage.save
local SaveButton = script.Parent
local CanSave = true

local Sound = script.select
local Cooldown = 5

SaveButton.MouseEnter:Connect(function()
    SaveButton.TextColor3 = Color3.new(1,1,1)
end)

SaveButton.MouseLeave:Connect(function()
    SaveButton.TextColor3 = Color3.new(1, 1, 1)
end)

local ValueDecrease
ValueDecrease = function(Value, GuiObject)
   Sound:Play()
   GuiObject.Text = Value

        repeat
            wait(1)
            Value = Value - 1

            GuiObject.Text = Value
        until Value == 0

        GuiObject.Text = "Save"
end


SaveButton.MouseButton1Click:Connect(function()
    if CanSave then
        CanSave = false

    SaveEvent:FireServer()

    ValueDecrease(60, SaveButton)

        CanSave = true
    end
end)


Server:

local SaveEvent = game.ReplicatedStorage.save

SaveEvent.OnServerEvent:Connect(function(Player)
    local Data = Player.Data
    local Id = "Money-"..Player.UserId -- Id has to be a string! 

    local Stat = Data.Money
    local DataStoreService = game:GetService("DataStoreService")

    local Money = DataStoreService:GetDataStore("Money")

    Money:SetAsync(Id, Stat.Value)
end

Ad

Answer this question