Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Data store script to store a part's property isn't working?

Asked by 2 months ago

I wanna make it so when the part is touched it changes it's color, also I want it to Data Store that part's Color when a player joins and when a player leaves, it's my first time trying to Data Store a part's property so of course it's going to fail the first time and that's why I'm here, to get some help from you Advanced scripters. :D

Code, a bit too long.

local datastoreservice = game:GetService("DataStoreService"):GetDataStore("BrickColorDatastore")
local part = game.Workspace.coolpart

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        part.BrickColor = BrickColor.new("Really red")
    end
end)


game.Players.PlayerAdded:Connect(function(player)
    local success,errmsg = pcall(function()         
        datastoreservice:SetAsync(player.UserId,part.BrickColor)
        part.BrickColor = BrickColor.new("Really red")
    end)

    if success then
        print("Part's color has been stored")
    else
        print("Part's color failed to store")
        warn(errmsg)
    end

end)


game.Players.PlayerRemoving:Connect(function(player)
    local success, errmsg = pcall(function()        
        datastoreservice:SetAsync(player.UserId,part.BrickColor)        
    end)
    if success then
        print("Data store successed")
    else
        print("Data store failed")
        warn(errmsg)
    end 
end)

Output errors:

So this is what the output is showing when I join the game in Roblox Studio:

Cannot store int in data store. Data stores can only accept valid UTF-8 characters.

Details.

I don't really know if its possible to data store a part's property but I believe it can.

The script is parented to ServerScriptService

The Part name is "coolpart". Probably not helpful of a detail.

Anyways, thanks for helping if you did or tried your best, I think it's a easy thing for you to solve this right..?

0
I think maybe you should be GETTING the Async when you join not setting it but that's just me AngryPlaayer 17 — 2mo
0
I tried that but sorry it didn't work but thanks for trying to help ZombieApocalypz3 35 — 2mo

1 answer

Log in to vote
-1
Answered by 2 months ago

I think it means you have to store the value in less than 8 characters. You should find a way to shorten the color value, here's a solution that may not be the GREATEST but, it fixes your problem.

local datastoreservice = game:GetService("DataStoreService"):GetDataStore("BrickColorDatastore")
local part = game.Workspace.coolpart

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        part.BrickColor = BrickColor.new("Really red")
    end
end)


game.Players.PlayerAdded:Connect(function(player)
    local color = Instance.new("IntValue")
    color.Name = "BrickColorStore"
    color.Parent = player

    local success, errmsg = pcall(function()  
        part.BrickColor = BrickColor.new("Really red")
        if part.BrickColor == "Really red" then
            player.BrickColorStore.Value = 1
            datastoreservice:SetAsync(player.UserId, player.BrickColorStore.Value)
        end
        if part.BrickColor == "Hot pink" then
            player.BrickColorStore.Value = 2 -- continue to do this for all the colors will be
            datastoreservice:SetAsync(player.UserId, player.BrickColorStore.Value)
        end
    end)

    if success then
        print("Part's color has been stored")
    else
        print("Part's color failed to store")
        warn(errmsg)
    end

end)


game.Players.PlayerRemoving:Connect(function(player)
    local success, errmsg = pcall(function()  
        part.BrickColor = BrickColor.new("Really red")
        if part.BrickColor == "Really red" then
            player.BrickColorStore.Value = 1
            datastoreservice:SetAsync(player.UserId, player.BrickColorStore.Value)
        end
        if part.BrickColor == "Hot pink" then
            player.BrickColorStore.Value = 2 -- continue to do this for all the colors your part will be
            datastoreservice:SetAsync(player.UserId, player.BrickColorStore.Value)
        end
    end)
end)

I don't know why you would need to get a value just to change the color of a part to a set color but I'm not you so I wouldn't know lol

0
Thank for helping, I know it's not a efficient way but I can take it :D ZombieApocalypz3 35 — 2mo
Ad

Answer this question