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

How do I make this Work, I've asked many questions on this script and it just won't work?

Asked by 7 years ago
local plumbob = script.Parent
for _, player in pairs(game.Players:GetPlayers()) do
    local character = player.Character
    if character then
        local head = character:FindFirstChild("Head")

        if head then 
        plumbob.Position = head.Position + Vector3.new (0, 2, 0)
    wait(.1)

    end

    end
end

2 answers

Log in to vote
0
Answered by 7 years ago

I'm guessing that you want this "plumbob" (I assume you are referring to the thing from The Sims) above each player, all the time. The algorithm you need (in English) is this:

--[[
Whenever a player joins the game:
    Make a plumb bob for them
    Do the following until they've left the game:
        Reposition their plumb bob over their head (if they have one)
        wait()
    (At this point, the player has left the game.)
    Destroy the cloned plumb bob
]]

You have to tell the script not only what to do, but when to do it (your first problem). Methods for that include using loops (ex "while true do") and events. The "whenever a player joins the game" is an event; the "do the following until they've left the game" is a loop with a condition (it could also be a loop with an event). ex:

game.Players.PlayerAdded:connect(Act1)

--Act1 will be called every time a player is added

while true do
    Act2()
    wait()
end

--Act2 will be called about 30 times per second (based on how quickly 'wait()' returns)

So, based on the English aglorithm I outlined above, The script should be in the workspace or ServerScriptService. Assuming you can put the plumb bob as a model in ServerStorage named "PlumbBob", the lua might be:

local plumbobOriginal = game.ServerStorage.Plumbob
game.Players.PlayerAdded:connect(function(player) --Every time a player is added, do the following for that player
    --Make a plumb bob for them
    local plumbob = plumbobOriginal:Clone()
    plumbob.Parent = workspace
    local head
    --Continue to reposition the plumbob until they leave
    while player.Parent ~= game.Players do --when player.Parent is not game.Players, the player has left the game
        --Reposition their plumb bob over their head (if they have one)
        head = player.Character and player.Character:FindFirstChild("Head")
        if head then
            plumbob.Position = head.Position + Vector3.new(0, 2, 0)
        end
        wait()
    end
    --Player has left
    plumbob:Destroy()
end

Now, a better solution would be to weld the plumbob to the player's head (and you can do this while leaving the 2 studs space above the head) - you would have to run that code every time the player got a new head (ex by respawning), but you wouldn't need the while loop, making it a much superior solution. I'm not very good with welds, but you could look it up yourself and experiment if you want.

0
I liked the way you explained it with the 'English Algorithm Method' macabe55101 38 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Hey macabe55101,

Apparently, you didn't get what all I said in your previous question about this. I will tell you what you have to do in the script and what you are doing wrong. First off, make the script outside of the plumbob. After wards, you want to define the plumbob at the top, like this, I am assuming that your script has the same parent as the plumbob:

local plumbob = script.Parent:FindFirstChild([plumbob's name in quotation marks])

After that, you want to create a new plumbob by cloning this one inside the Generic for loop. This is how you would do that:

for _, player in next, game:GetService("Players"):GetPlayers() do
    local new_plumbob = plumbob:Clone()
end

After you clone it, if I were you, I'd parent it to the Character of the Player right away just to make it seem more realistic and also, to get rid of worrying about it's parent. After I parent it to the Character, I would make a variable for Head and then, I would do a coroutine loop inside of that with a while loop inside it. Something like this:

for _, player in next, game:GetService("Players"):GetPlayers() do
    local new_plumbob = plumbob:Clone()
    local character = player.Character
    coroutine.wrap(function()
        while wait() do
            local head = character:FindFirstChild("Head")
            new_plumbob.Position = head.Position + Vector3.new(0, 2, 0)
        end
    end)
end

Well, that's how I would go about doing it. I hope I helped in one way or another and have a great day/night.

~~ KingLoneCat

Answer this question