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

Unable to cast value to std::string?

I have a script that is supposed to do stuff like team only tools but I get "Unable to cast value to std::string" every time I try to use it. Here's the script.

game.Players.PlayerAdded:connect(function(player)
	local starterFolder = game.Teams:findFirstChild(player.TeamColor)
    player.CharacterAdded:connect(function(character)
        if starterFolder then
        	starterFolder.PoliceCard:Clone().Parent = player.Backpack
			starterFolder.AK47:Clone().Parent = player.Backpack
        end
    end)
end)

7 comments

0

how i make code block???????

0

code blocks are just two sets of 17 ~s

0

Edited for code block.

0

And for for code errors, TeamColor is a UserData, while FindFirstChild wants a string

0

Actually, you can get away with two sets of six ~'s or simply a four space indentation of the entire code block.

0

Players have a team property which you can use instead of what you are currently using for line 2

0

the cast value junk is the only error i get

2 answers

Looks like the issue is that you're using an object that can't hold items as a place to store items. A fix to this would be to use something like ServerStorage (or replicated if you're using a local script), which is made specifically for this purpose.

Here's an edit to your script, which requires you to make a folder inside of ServerStorage, with the name of ["Folder_Items"]. Make sure you put all the weapons in there:

local serStorage = game:GetService('ServerStorage')
local starterFolder = serStorage:WaitForChild('Folder_Items')

game.Players.PlayerAdded:connect(function(player)
	starterFolder.PoliceCard:Clone().Parent = player.Backpack
        starterFolder.AK47:Clone().Parent = player.Backpack
end)

1 comment

0

The issue could have also been with using the playeradded function inside of itself, I don't have my laptop so sadly I can't test this );

I found a better script. Why not be more helpful guys?

function teamFromColor(color)
for _,t in pairs(game:GetService("Teams"):GetChildren()) do
if t.TeamColor==color then return t end
end
return nil
end

function onSpawned(plr)
local tools = teamFromColor(plr.TeamColor):GetChildren()
for _,c in pairs(tools) do
c:Clone().Parent = plr.Backpack
end
end

function onChanged(prop,plr)
if prop=="Character" then
onSpawned(plr)
end
end

function onAdded(plr)
plr.Changed:connect(function(prop)
onChanged(prop,plr)
end)
end

game.Players.PlayerAdded:connect(onAdded)

1 comment

0

please, take my downvote. don't "find" scripts. make them. furthermore, your error is a C error, and C errors are also readable. you provided the first argument of FindFirstChild as a userdata value. userdatas cannot be casted into strings. brickcolor (or rather, any color) is a userdata object. next time you make a question, please read the error.

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