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

Why doesn't the script change properties according to if statement?

Asked by 6 years ago

Hey!

I'm trying to make a buyable house but for some reason this if statement doesn't work. Help?

The Owner value is set to my name so the script should change the door's properties

Script:

owner = script.Parent.Parent.Owner

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if hit.Parent.Name == owner then
            script.Parent.Transparency = 0.5
            script.Parent.CanCollide = false
        end
    end
end)

1 answer

Log in to vote
0
Answered by
Ryrasil 30
6 years ago
owner = script.Parent.Parent.Owner --an object

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if hit.Parent.Name == owner then --hit.Parent.Name is a string and owner is an object you can't compare them
            script.Parent.Transparency = 0.5
            script.Parent.CanCollide = false
        end
    end
end)

The problem here is that you're comparing a string to a userdatum object

owner = script.Parent.Parent.Owner.Value -- I'm assuming the owner object is a StringValue so to get the value you just get the value property

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if hit.Parent.Name == owner then --here you compare 2 strings now so that should work
            script.Parent.Transparency = 0.5
            script.Parent.CanCollide = false
        end
    end
end)

This should be a fixed version!

0
But your script caches the first value of Owner for ever. You should rather get its value each time. Amiaa16 3227 — 6y
0
I added .Value to the owner variable and it didn't work but I did what Kiriot said and added .Value to the if statement that is comparing the 2 strings and it worked so Thank You both :D bruceywayne 35 — 6y
Ad

Answer this question