"attempt to iterate over a string value"

  1. What do you want to achieve?
    I’m trying to make an egg hatching system

  2. What is the issue?
    Whenever the player presses E, it returns a string value instead of the selectedEgg and i’ve no idea how to fix it.

  3. What solutions have you tried so far?
    I’ve tried typing the error on the dev forum and almost nothing came out.

This is the function in the local script (the error is on the same line as “for _, pet in hatch do”:

local function Hatch(hatchAmount) -- universal function to hatch eggs
	print("5") -- prints
	if selectedEgg then
		local timeValue = player.leaderstats.Time.Value
		local price = eggs:FindFirstChild(selectedEgg):GetAttribute("Price") * hatchAmount -- gets the price of the egg * the hatch amount
		
		local afford = timeValue >= price -- if player time is lower than the price then afford = false if it's higher then it's equal to true
		
		print("6") -- print
		
		if afford then
			local hatch = hatchPets:InvokeServer(selectedEgg)
			if hatch then
				for _, pet in hatch do -- this is where the error is
					hatchAnim(selectedEgg, pet)
					print("7") -- doesn't print
				end
			end
		end
	end
end

This is the server side, where the remote function is triggered:

local function choosePet(selectedEgg)
	local chance = math.random(1, totalWeight)
	local counter = 0
	
	for _, pet in pairs(pets:FindFirstChild(selectedEgg):GetChildren()) do
		counter += pet:GetAttribute("Rarity")
		if chance <= counter then
			return pet.Name
		end
	end
end


hatchPets.OnServerInvoke = function(player, selectedEgg)
	totalWeight = 0
	
	for _, pet in pairs(pets:FindFirstChild(selectedEgg):GetChildren()) do
		totalWeight += pet:GetAttribute("Rarity") -- the total rarity doesn't need to be at exactly 100%, if it's at 99.1%, it's still good.
	end
	
	
	local chosenPet = choosePet(selectedEgg)
	
	return chosenPet
end

Would be really appreciated if anyone could help. Thanks in advance

2 Likes

If you print hatch and pet before the animation, what does it print?

for _, pet in hatch do -- this is where the error is
    print("hatch is a: " .. typeof(hatch)) --what is hatch at this point?
    print("Pet: ".. pet) -- what does this print?
    hatchAnim(selectedEgg, pet)
    print("7") -- doesn't print
end

Are you sure the server script is returning what you’re expecting? What is choosePet() and is it returning a string by chance?

local chosenPet = choosePet(selectedEgg)
print("ChosenPet: " .. chosenPet .. ", is a " .. typeof(chosenPet))
return chosenPet
2 Likes

it stops at for _, pet in hatch do

and yes, It is return the pet.Name (I edited the server script so you can see when the choosePet is)

it prints out:

 ChosenPet: Bear, is a string 

Do you have any way to get the pet.Name and turning it into the ui? I thought just getting the pet.Name was enough to duplicate it and get it in the player’s screen

1 Like

Don’t look like you was too far off. It looks like the Server function is returning a string, while the Client function is expecting an array (table). So let’s change a few things around here …

change: local hatch = hatchPets:InvokeServer(selectedEgg)
to: local hatch = hatchPets:InvokeServer(selectedEgg, hatchAmount)

and the hatchPets.OnServerInvoke fuction to:

hatchPets.OnServerInvoke = function(player, selectedEgg, hatchAmount)
	totalWeight = 0
	
	for _, pet in pairs(pets:FindFirstChild(selectedEgg):GetChildren()) do
		totalWeight += pet:GetAttribute("Rarity")
	end
	
	local chosenPets = {}
	for i = 1, hatchAmount do
		table.insert(chosenPets, choosePet(selectedEgg))
	end
	
	return chosenPets
end

Other than that it looks like you had it. Can’t really test anything here so you’ll have to do that.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.