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

Need Help With Roblox Datastore Service. Error "Unable To Cast Value To Function"?

Asked by 5 years ago
Edited 5 years ago

I am trying to make a game where the value of points gets saved. I check in game and the points values goes from 375 to 475 and then I exit. When I load back in the points goes back to 375. I get the error "unable to cast value to function"

   local DataStoreService = game:GetService("DataStoreService")

local CreditsStore = DataStoreService:GetDataStore("CreditsStore")



local saveLoop = coroutine.create(function()

while true do

wait(300)

for i,v in pairs(game:GetService("Players"):GetChildren())do

wait()

if v then

if v.Character:FindFirstChild("Humanoid") then

CreditsStore:SetAsync(v.UserId.."-CreditsStore",v.leaderstats.Points.Value)

end

end

end

end

end)



game:GetService("Players").PlayerAdded:Connect(function(plr)

local leader = Instance.new("Folder",plr)

leader.Name = "leaderstats"

local points = Instance.new("IntValue",leader)

points.Name = "Points"

pcall(function()

points.Value = CreditsStore:GetAsync(plr.UserId.."-CreditsStore") or 0

end)

coroutine.resume(saveLoop)

end)

game:GetService("Players").PlayerRemoving:Connect(function(plr)

pcall(function()

CreditsStore:SetAsync(plr.UserId.."-CreditsStore",plr.leaderstats.Points.Value)

end)

end)
0
The same thing is happening to me, when you get a solution, I will be here, if I do, I will comment here. Araknala 14 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Since you are using a pcall(function(), any errors will not make the script stop thus, making it not display the error in the output and continue the scripts even though there is an error.

To make it display the error(s) do this:

local Success, errorMessage = pcall(function()
    -- Your code
end)

if Success then
    -- If it does not have any error
    print(Success) -- Prints true since no error
else
    -- If there is an error
    print(errorMessage) -- prints the error out
end

Hopes this helps and found out what is the error and work your way around it! :P

0
I get the error unable to cast value to function jakebball2014 84 — 5y
Ad

Answer this question