So my main question is, how would I transfer information from the client to the server, for example, a number from the client to the server, because whenever I try, the information turns out as nil, here is my localscript,
local currentcolor
script.Parent.MouseButton1Click:Connect(function()
currentcolor = script.Parent.Parent.ColourPreview.BackgroundColor3
game.ReplicatedStorage.ChangeColor:FireServer(currentcolor)
end)
and here is the script receiving, or atleast a part of a script,
game.ReplicatedStorage.ChangeColor.OnServerEvent:Connect(function(plr, color)
leaderstats.CurrentColor.Value = color
end)
local currentcolor
script.Parent.MouseButton1Click:Connect(function()
-- Checking for ColourPreview
local colourPreview = script.Parent.Parent:FindFirstChild("ColourPreview")
if colourPreview then
currentcolor = colourPreview.BackgroundColor3
game.ReplicatedStorage.ChangeColor:FireServer(currentcolor)
print("The event was sent successfully")
else
warn("ColourPreview is missing")
end
end)
On the server side you can change your code to the following:
game.ReplicatedStorage.ChangeColor.OnServerEvent:Connect(function(plr, color)
if typeof(color) == "Color3" then
local leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats then
local currentColor = leaderstats:FindFirstChild("CurrentColor")
if currentColor and currentColor:IsA("ValueBase") then
currentColor.Value = color
else
warn("CurrentColor property not found or not an ValueBase")
end
else
warn("leaderstats missing from player")
end
else
warn("The passed color value is not of type Color3")
end
end)
You should also ensure that ReplicationStorage.ChangeColor exists and has been replicated correctly on the client. It is recommended that you use the WaitForChild() method instead of using direct retrieval.
I tried it and this is what the output looked like,
20:49:31.087 The event was sent successfully - Client - LocalScript:9
20:49:31.096 CurrentColor property not found or not an ValueBase - Server - OT:92
local statList = {
{Name = "CurrentColor", Type = "Color3Value", StarterValue = Color3.new(1, 1, 1)};
}
local Players = game:GetService("Players")
local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerDataOT")
local function SavePlayer(player)
local dataToSave = {}
for _, stat in pairs(player.OT:GetChildren()) do
dataToSave[stat.Name] = stat.Value
end
local success, err = pcall(function()
Datastore:SetAsync(player.UserId, dataToSave)
end)
if not success then
warn("[!] Error saving data for " .. player.Name)
end
end
local function NewStat(statType, statName, statParent, starterValue)
local new = Instance.new(statType)
new.Name = statName
new.Value = starterValue or 0 -- Set default value or starter value
new.Parent = statParent
return new
end
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("BoolValue")
leaderstats.Name = "OT"
leaderstats.Parent = player
for _, statInfo in pairs(statList) do
local success, err = pcall(function()
NewStat(statInfo.Type, statInfo.Name, leaderstats, statInfo.StarterValue)
end)
if not success then
warn("Error creating stat " .. tostring(statInfo.Name) .. ": " .. tostring(err))
end
end
local data
local success, err = pcall(function()
data = Datastore:GetAsync(player.UserId)
end)
if success and data then
for statName, statValue in pairs(data) do
if leaderstats:FindFirstChild(statName) then
leaderstats[statName].Value = statValue
end
end
elseif err then
warn("[!] Error loading data for " .. player.Name .. ": " .. tostring(err))
end
-- Check and set starter value if current value is 0
for _, stat in pairs(leaderstats:GetChildren()) do
if stat.Value == 0 then
for _, statInfo in pairs(statList) do
if statInfo.Name == stat.Name then
stat.Value = statInfo.StarterValue
break
end
end
end
end
spawn(function()
local plr = player
while wait(0.1) do
if game.Workspace.GameKarts:FindFirstChild(plr.Name) then
local Kart = game.Workspace.GameKarts:FindFirstChild(plr.Name)
for i, part in pairs(Kart.Color:GetChildren()) do
part.Color3 = leaderstats.CurrentColor.Value
end
end
end
end)
game.ReplicatedStorage.ChangeColor.OnServerEvent:Connect(function(plr, color)
if typeof(color) == "Color3" then
local leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats then
local currentColor = leaderstats:FindFirstChild("CurrentColor")
if currentColor and currentColor:IsA("ValueBase") then
currentColor.Value = color
else
warn("CurrentColor property not found or not an ValueBase")
end
else
warn("leaderstats missing from player")
end
else
warn("The passed color value is not of type Color3")
end
end)
end)
Players.PlayerRemoving:Connect(SavePlayer)
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
SavePlayer(player)
end
end)
This is the full script if you were wondering, idk if this helps
I don’t understand why you are deliberately complicating your code instead of clearly creating a leaderboard?
In any case, try the following code:
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
for _, statInfo in pairs(statList) do
local success, err = pcall(function()
NewStat(statInfo.Type, statInfo.Name, leaderstats, statInfo.StarterValue)
end)
if not success then
warn("Error creating stat " .. tostring(statInfo.Name) .. ": " .. tostring(err))
end
end
local data
local success, err = pcall(function()
data = Datastore:GetAsync(player.UserId)
end)
if success and data then
for statName, statValue in pairs(data) do
if leaderstats:FindFirstChild(statName) then
leaderstats[statName].Value = statValue
end
end
elseif err then
warn("[!] Error loading data for " .. player.Name .. ": " .. tostring(err))
end
-- Setting initial values
for _, stat in pairs(leaderstats:GetChildren()) do
if stat.Value == 0 then
for _, statInfo in pairs(statList) do
if statInfo.Name == stat.Name then
stat.Value = statInfo.StarterValue
break
end
end
end
end
end)
game.ReplicatedStorage.ChangeColor.OnServerEvent:Connect(function(plr, color)
if typeof(color) == "Color3" then
local leaderstats = plr:FindFirstChild("leaderstats")
if leaderstats then
local currentColor = leaderstats:FindFirstChild("CurrentColor")
if currentColor and currentColor:IsA("Color3Value") then
currentColor.Value = color
else
warn("CurrentColor property not found or not a Color3Value")
end
else
warn("leaderstats missing from player")
end
else
warn("The passed color value is not of type Color3")
end
end)