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 4 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.

01script.Parent.Touched:Connect(function(hit)
02    local h = hit.Parent:FindFirstChild('Humanoid')
03    if h ~= nil then
04        local chr = hit.Parent
05        local plr = game.Players[chr.Name]
06        if plr.leaderstats.Stage >= script.Parent.Name then
07        else
08            plr.leaderstats.Stage = script.Parent.Name
09        end
10    end
11end)
0
Is the stage value that you're using is a Number Value? johncrazy1rb 11 — 4y
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 — 4y

3 answers

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

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

01script.Parent.Touched:Connect(function(hit)
02    local h = hit.Parent:FindFirstChild('Humanoid')
03    if h ~= nil then
04        local chr = hit.Parent
05        local plr = game.Players[chr.Name]
06        if tostring(plr.leaderstats.Stage.Value) >= script.Parent.Name then
07        else
08            tostring(plr.leaderstats.Stage.Value) = script.Parent.Name
09        end
10    end
11end)
0
bruh you answered line 9 i have the problem on 6 also use ~~~~~ for code blocks PufferfishDev 49 — 4y
0
I can barely read this mess, because its not a code block. vincentthecat1 199 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

The solution is this :

01script.Parent.Touched:Connect(function(hit)
02    local h = hit.Parent:FindFirstChild('Humanoid')
03    if h ~= nil then
04        local chr = hit.Parent
05        local plr = game.Players[chr.Name]
06        if plr.leaderstats.Stage.Value >= script.Parent.Name then
07        else
08            plr.leaderstats.Stage.Value = script.Parent.Name
09        end
10    end
11end)

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 4 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.

01script.Parent.Touched:Connect(function(hit)
02    local h = hit.Parent:FindFirstChild('Humanoid')
03    if h ~= nil then
04        local chr = hit.Parent
05        local plr = game.Players[chr.Name]
06        if plr.leaderstats.Stage.Value >= script.Parent.checkpointNumber.Value then
07        else
08            plr.leaderstats.Stage.Value = script.Parent.checkpointNumber.Value
09        end
10    end
11end)

Answer this question