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

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

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

✓ Accepted answer
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!

2 comments

0

But your script caches the first value of Owner for ever. You should rather get its value each time.

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

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