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

how to get player & character through server script?

Asked by 4 years ago
Edited 4 years ago

here i have created a script that is supposed to weld a model to the player humanoid root part

local rStorage = game:GetService("ReplicatedStorage")
local morphItem = rStorage:WaitForChild("Morph")

local maxMorphDistance = 5
morphItem.OnServerEvent:Connect(function(player, item)
    local distanceFromItem = player:DistanceFromCharacter(item.Morpher.Position)
    if distanceFromItem <= maxMorphDistance then
        -- morphcode here
        local copy = item:Clone()
        copy.Parent = item.Parent
        copy:SetPrimaryPartCFrame(CFrame.new(0, 50, 0))

        local part = item.Morpher
        local Weld = Instance.new("Weld",part)
        print("done")

        -- problem starts below
        game:GetService("Players").PlayerAdded:Connect(function(player)
            player.CharacterAdded:Connect(function(character)
                print("a")
                local torso =   game.Workspace:WaitForChild(player):WaitForChild("HumanoidRootPart")

                part.CFrame = torso.CFrame 
                Weld.Part0 = part
                Weld.Part1 = torso
                print("DONE".. player)
            end)
        end)
    end
end)

im having trouble getting the player, the only thing printed to console is "done". can anyone help?

3 answers

Log in to vote
0
Answered by 4 years ago
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)

    --Script

    end)
end)

im use all time this script

0
this isnt working, i used print statements in both functions to debug and neither print statements were outputted to console Boungst 5 — 4y
Ad
Log in to vote
0
Answered by
3F1VE 257 Moderation Voter
4 years ago

btw in in line 26 it only prints done because you didint do this :

print("DONE"..player.Name)
0
even with player.Name its still not being printed Boungst 5 — 4y
0
oh check the rest of the script then 3F1VE 257 — 4y
Log in to vote
0
Answered by 4 years ago

I think your problem could be because you're using PlayerAdded, that only runs when a player joins and you're firing the event BEFORE that player added even has a chance to run when it's supposed to, I'd suggest doing a for i,v inpairs(game:GetService("Players")) loop then just searching for the player through that.

0
FYI: That's just a hunch, I'm not a professional scripter by any means. adamson239 12 — 4y

Answer this question