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

'attempt to compare string and Instance'. How to fix?

Hello there. I am trying to make a difficulty chart obby and I am trying to make my own checkpoint system. I cannot figure out why on line 6 it outputs 'attempt to compare string and Instance'. Also, I need a little help moving a character to a checkpoint after they die. Here is my code.

script.Parent.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild('Humanoid')
	if h ~= nil then
		local chr = hit.Parent
		local plr = game.Players[chr.Name]
		if plr.leaderstats.Stage >= script.Parent.Name then
		else
			plr.leaderstats.Stage = script.Parent.Name
		end
	end
end)

2 comments

0

Is the stage value that you're using is a Number Value?

0

Stage.Value, "attempt to compare string and instance" means that stage is an instance and script.Parent.Name is a string. Solution: Stage.Value lines 8 and 6

3 answers

You are trying to compare the instance of the value, try doing Stage.Value

script.Parent.Touched:Connect(function(hit)
    local h = hit.Parent:FindFirstChild('Humanoid')
    if h ~= nil then
        local chr = hit.Parent
        local plr = game.Players[chr.Name]
        if tostring(plr.leaderstats.Stage.Value) >= script.Parent.Name then
        else
            tostring(plr.leaderstats.Stage.Value) = script.Parent.Name
        end
    end
end)

2 comments

0

bruh you answered line 9 i have the problem on 6 also use ~~~~~ for code blocks

0

I can barely read this mess, because its not a code block.

The solution is this :

script.Parent.Touched:Connect(function(hit)
    local h = hit.Parent:FindFirstChild('Humanoid')
    if h ~= nil then
        local chr = hit.Parent
        local plr = game.Players[chr.Name]
        if plr.leaderstats.Stage.Value >= script.Parent.Name then
        else
            plr.leaderstats.Stage.Value = script.Parent.Name
        end
    end
end)

You need to add a Value in plr.leaderstats.Stage because you're trying to compare the "value container" to the checkpoint number. Let me explain : Let's say you have a variable with a number inside, this will be a box with some balls containing the value inside of it. With your script you're trying to compare the box with the CheckpointNumber, this will throw an error. You need to do plr.leaderstats.Stage.Value so that it will compare the number of balls inside the box (your variable) with the CheckPointNumber.

Hope this helped :)

Fixed it myself. Put a value along with the script inside the part. New script looks like this for anybody who wants to use it.

script.Parent.Touched:Connect(function(hit)
	local h = hit.Parent:FindFirstChild('Humanoid')
	if h ~= nil then
		local chr = hit.Parent
		local plr = game.Players[chr.Name]
		if plr.leaderstats.Stage.Value >= script.Parent.checkpointNumber.Value then
		else
			plr.leaderstats.Stage.Value = script.Parent.checkpointNumber.Value
		end
	end
end)

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