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

Why is my face change script not working properly, when it should be?

Asked by
sad_eyez 162
7 years ago

I have a folder in replicatedstorage called "Faces" and each of the faces has a number as the name and i have a doubleconstrainedvalue to change and match that number and if it macthes the number it should be changing the face, but it'snot, and the left and right btton wont even work right either, can anyone help me, and there is absolutely no error so this is confusing as hell.

CODE:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local head = char:WaitForChild("Head")
local face = head:WaitForChild("face")

local left = script.Parent:WaitForChild("Left")
local right = script.Parent:WaitForChild("Right")
local nof = script.Parent:WaitForChild("NumberOfFaces")

local rs = game:GetService("ReplicatedStorage")
local faces = rs:WaitForChild("Faces")

left.MouseButton1Click:connect(function()
    nof.Value = nof.Value +1
    wait(0.2)
end)

right.MouseButton1Click:connect(function()
    nof.Value = nof.Value -1
    wait(0.2)
end)

while wait() do
    script.Parent.Text = "FACE "..nof.Value
    for i,v in pairs(faces:GetChildren()) do
        if v.Name == nof.Value then
            face.Texture = v.Texture
        end
    end
end

0
Try print debugging, see which parts actually run. I suppose it's with your if statement on line 26 as you check if the face name is the same as the number of faces. RubenKan 3615 — 7y

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

Problem

Your problem has to do with a difference in datatypes on line 26.

Since 'nof' is a DoubleConstrainedValue and 'v.Name' is a string, line 26's if statement will never pass. And the face will never change.


Solution

You can fix this using the tostring function. The tostring function takes in any datatype, and converts it to a string.

if v.Name == tostring(nof.Value) then --tostring usage
    face.Texture = v.Texture
end


Efficiency changes

The way above works, but isn't quite as efficient as it could be. Your while loop is going to run forever, constantly iterating through the faces and checking them.

An alternative, easier, method to do this would be to use the Changed event on the 'nof' value. The Changed event fires whenever the specified value is changed - and it returns the new value. You can use the FindFirstChild function to search through 'faces' and get the correct face.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local head = char:WaitForChild("Head")
local face = head:WaitForChild("face")
local left = script.Parent:WaitForChild("Left")
local right = script.Parent:WaitForChild("Right")
local nof = script.Parent:WaitForChild("NumberOfFaces")
local rs = game:GetService("ReplicatedStorage")
local faces = rs:WaitForChild("Faces")

left.MouseButton1Click:connect(function() nof.Value = nof.Value +1 end)
right.MouseButton1Click:connect(function() nof.Value = nof.Value -1 end)

nof.Changed:Connect(function(val)
    script.Parent.Text = "FACE "..tostring(val)
    local v = faces:FindFirstChild(tostring(val))
    face.Texture = v.Texture
end)
Ad

Answer this question