Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

1 comment

0

The same thing is happening to me, when you get a solution, I will be here, if I do, I will comment here.

1 answer

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

1 comment

0

I get the error unable to cast value to function

This discussion is preserved as it was. New answers, comments, and votes are closed.