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

How to properly edit a script.Parent's property?

So I wrote a script that makes an icon visible when the player gets to a certain speed (in this case 11). I don't get script errors but it just doesnt seem to work for some reason. What am I doing wrong?

local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait() or Player.Character
local CharacterInfo = Character:WaitForChild("Humanoid")
local PlayerSpeed = CharacterInfo:FindFirstChild("Running")
	if  PlayerSpeed == 11 then
		script.Parent.ImageTransparency = 0
    else	
		script.Parent.ImageTransparency = 1		
    end

1 answer

✓ Accepted answer

Compare Instance.Value, not the Instance itself

PlayerSpeed is an Instance. You are comparing Instance with number 11 which obviously won't pass the if statement, you need to get the Value of the Running Instance. To do that all you have to do is change PlayerSpeed == 11 to PlayerSpeed.Value == 11

local PlayerSpeed = CharacterInfo:FindFirstChild("Running")
    if  PlayerSpeed.Value == 11 then
        script.Parent.ImageTransparency = 0
    else    
        script.Parent.ImageTransparency = 1     
    end

GetPropertyChangedSignal?

The other possible problem is that you want the icon to be visible every time the speed changes, right? I got it like this, for that you would use GetPropertyChangedSignal, it's a function of every Instance which returns event which fires when given property of it changes. In your case it would look something like this:

local PlayerSpeed = CharacterInfo:FindFirstChild("Running")
PlayerSpeed:GetPropertyChangedSignal('Value'):Connect(function()
    if  PlayerSpeed.Value == 11 then
        script.Parent.ImageTransparency = 0
    else    
        script.Parent.ImageTransparency = 1     
    end
end)

But it would need some improvements, this is just as an example.

Sources

GetPropertyChangedSignal

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