65 until 69
uqbwgduewevbcdkshjclweujdbfckdsw
65 until 69
uqbwgduewevbcdkshjclweujdbfckdsw
When I test the script I gave you, Iâm not receiving the same error so make sure you havenât accidentally changed a value somewhere
How does your script look like now?
it still says that
I need to see the whole script, please
here
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local dataStore = DataStoreService:GetDataStore("PlayerData") -- You can rename this if you want
local canSave = {}
Players.PlayerAdded:Connect(function(player)
local success, values = pcall(dataStore.GetAsync, dataStore, player.UserId)
if success then
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = values and values.Coins
Coins.Parent = player
local Gems = Instance.new("IntValue")
Gems.Name = "Gems"
Gems.Value = values and values.Gems
Gems.Parent = player
canSave[player] = true
else
warn(values)
end
end)
local function saveData(player)
if canSave[player] then
local success, result = pcall(dataStore.SetAsync, dataStore, player.UserId, {
Coins = player.Coins.Value,
Gems = player.Gems.Value
})
canSave[player] = nil
if not success then warn(result) end
end
end
Players.PlayerRemoving:Connect(saveData)
if RunService:IsStudio() then
game:BindToClose(function()
task.wait(4)
end)
else
game:BindToClose(function()
local x, y = 0, 0
for _, player in Players:GetPlayers() do
x += 1
coroutine.wrap(function()
saveData(player)
y += 1
end)()
end
repeat task.wait() until y == x
end)
end
coroutine.wrap(function(player)
saveData(player)
y += 1
end)(player)
repeat task.wait() until y == x
end)
end
this is the part that says error
do i put the new code to the wrong place?
When I copy and paste and test the script myself, it works without error
Iâm noticing that the formatting is different so did you modify the script?
i dont think so, let me test it again
wait, what type of script does this script is for? is it serverscript?
Itâs a server Script inside of ServerScriptService
maybe my pleacement is the one thats causing an error?
When I place mine in a Folder itâs working as well so the issueâs quite strange
Edit: @Xxmaster_farterxX I found what the problem is:
You I accidentally wrote the coroutine twice so remove the second one and make sure the code is exactly as follows:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local dataStore = DataStoreService:GetDataStore("PlayerData") -- You can rename this if you want
local canSave = {}
Players.PlayerAdded:Connect(function(player)
local success, values = pcall(dataStore.GetAsync, dataStore, player.UserId)
if success then
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = values and values.Coins
Coins.Parent = player
local Gems = Instance.new("IntValue")
Gems.Name = "Gems"
Gems.Value = values and values.Gems
Gems.Parent = player
canSave[player] = true
else
warn(values)
end
end)
local function saveData(player)
if canSave[player] then
local success, result = pcall(dataStore.SetAsync, dataStore, player.UserId, {
Coins = player.Coins.Value,
Gems = player.Gems.Value
})
canSave[player] = nil
if not success then warn(result) end
end
end
Players.PlayerRemoving:Connect(saveData)
if RunService:IsStudio() then
game:BindToClose(function()
task.wait(4)
end)
else
game:BindToClose(function()
local x, y = 0, 0
for _, player in Players:GetPlayers() do
x += 1
coroutine.wrap(function()
saveData(player)
y += 1
end)()
end
repeat task.wait() until y == x
end)
end
its working!!! thanks a bunch!
It was my fault actually, so sorry about that
wait, how do i change the value using a code? i tested using my existing localscript and it doesent save.
It will depend on when youâd like to change it during your game, but youâll need to remember to change the value on the server not in LocalScripts else the server wonât see the new value
Youâll need to use RemoteEvents to tell the server to change the value
do when a remote event fire, the server script doesent change all players value?
You can make it change the value for every player in the game if you want by doing this:
remoteEvent.OnServerEvent:Connect(function()
for _, player in game.Players:GetPlayers() do
if player:FindFirstChild("Coins") then
player.Coins += 1
player.Gems += 1
end
end
end)
no, i want it to only change to the player that fired the event