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

Distance checking script clones a gui to the player if they are close enough, but it doesn't work?

Asked by
pwnd64 106
8 years ago

This was working perfectly fine until recently, but now it doesn't. I think I've found the issue, and that's that it doesn't find the player's character. I have no idea how to fix that because obviously every player has a character.

shopblock = script.Parent

shopdistance = 10

while true do

wait(.1)

    for i,v in pairs(game.Players:GetPlayers()) do

        if v:FindFirstChild("Character") then

    char = v:FindFirstChild("Character")

            if char:FindFirstChild("Torso") then

                torso = char:FindFirstChild("Torso")        

                if (shopblock.Position-torso.Position).magnitude <= shopdistance then

                    if not v.PlayerGui:FindFirstChild("raidershopgui") then

                        print("Cloning "..shopblock.Name.."gui to the player")

                        game.ReplicatedStorage[shopblock.Name.."gui"]:Clone().Parent = v.PlayerGui

                    end

                elseif (shopblock.Position-torso.Position).magnitude > shopdistance and v.PlayerGui:FindFirstChild(shopblock.Name.."gui") then

                    v.PlayerGui[shopblock.Name.."gui"]:Destroy()

                    print("Destroyed "..shopblock.Name.."gui from the player")

                end

            end 

        end

    end

end

Thanks for reading.

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

I believe your problem is that you are calling the method FindFirstChild which looks for descendants, when you actually want to check the value of the property of the player Character.

Changing v:FindFirstChild("Character") to v.Character in your if statement should fix your problem.

shopblock = script.Parent
shopdistance = 10

while true do
    for _, player in next, game.Players:GetPlayers()
        if player.Character and player.Character:FindFirstChild("Torso") then
            torso = player.Character.Torso
            distance = (shopblock.Position - torso.Position).magnitude
            if distance <= shopdistance then
                if not player.PlayerGui:FindFirstChild("raidershopgui") then
                    game.ReplicatedStorage[shopblock.Name.."gui"]:Clone().Parent = v.PlayerGui
                end
            else
                if v.PlayerGui:FindFirstChild(shopblock.Name.."gui") then
                    v.PlayerGui[shopblock.Name.."gui"]:Destroy()
                end
            end
        end
    end
    wait()
end
0
Thanks. It worked! pwnd64 106 — 8y
Ad

Answer this question