Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 3 years ago

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)
0
Is the stage value that you're using is a Number Value? johncrazy1rb 11 — 3y
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 greatneil80 2647 — 3y

3 answers

Log in to vote
0
Answered by 3 years ago
Edited by imKirda 3 years ago

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)
0
bruh you answered line 9 i have the problem on 6 also use ~~~~~ for code blocks PufferfishDev 49 — 3y
0
I can barely read this mess, because its not a code block. vincentthecat1 199 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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 :)

Log in to vote
0
Answered by 3 years ago

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)

Answer this question