This is my code inside the button
function Convert() local plr = game.Players.PlayerAdded:Wait() if plr.Leaderstats.Convert.Value > plr.Leaderstats.Robux.Value then script.Parent.Parent.ConvertBox.Text = "Invalid conversion amount!" wait(2) script.Parent.Parent.ConvertBox.Text = "" else plr.Leaderstats.Tix.Value-=plr.Leaderstats.Convert.Value plr.Leaderstats.Robux.Value+=plr.Leaderstats.FinalConvert.Value end end script.Parent.MouseButton1Click:Connect(Convert)
this is all inside a server script. This is because my data store wont work if it is a local script. How would I fix this so it can stay inside a server script?
Sorry bud but you really need a LocalScript
in order for the button to work. To make your data store work, you can use a RemoteEvent
to change the value in server and a RemoteFunction
to get the value of the player from the server (because if you get it to the client it might not be the same to the server since hackers can change anything ONLY to the client).
First, you create a RemoteEvent
and a RemoteFunction
inside ReplicatedStorage
. Name the RemoteEvent
SetValue and the RemoteFunction
GetValue.
Next, you will create a script in ServerScriptService
and name it RemoteHandler.
Then, you will create a listener for SetValue
and a callback function for GetValue
in RemoteHandler
.
local ReplicatedStorage = game:GetService("ReplicatedStorage") local GetValue = ReplicatedStorage.GetValue local SetValue = ReplicatedStorage.SetValue GetValue.OnServerInvoke = function(player, valueName) local leaderstats = player:FindFirstChild("Leaderstats") if leaderstats then local value = leaderstats:FindFirstChild(valueName) if value then -- if it exists if value:IsA("ValueBase") then return value.Value end end end end SetValue.OnServerEvent:Connect(function(player, valueName, valueSet) local leaderstats = player:FindFirstChild("Leaderstats") if leaderstats then local value = leaderstats:FindFirstChild(valueName) if value then -- if it exists if value:IsA("ValueBase") then value.Value = valueSet end end end end)
Lastly, you can use them in the LocalScript
and it should look like this:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local GetValue = ReplicatedStorage:WaitForChild("GetValue") local SetValue = ReplicatedStorage:WaitForChild("SetValue") function Convert() if GetValue:InvokeServer("Convert") > GetValue:InvokeServer("Robux") then script.Parent.Parent.ConvertBox.Text = "Invalid conversion amount!" task.wait(2) script.Parent.Parent.ConvertBox.Text = "" else SetValue:FireServer("Tix", (GetValue:InvokeServer("Tix") - GetValue:InvokeServer("Convert"))) SetValue:FireServer("Robux", (GetValue:InvokeServer("Robux") + GetValue:InvokeServer("FinalConvert"))) end end script.Parent.MouseButton1Click:Connect(Convert)