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

how to get player & character through server script?

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

local Players = game:GetService("Players")
 
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)

	--Script

	end)
end)

im use all time this script

1 comment

0

this isnt working, i used print statements in both functions to debug and neither print statements were outputted to console

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

print("DONE"..player.Name)

2 comments

0

even with player.Name its still not being printed

0

oh check the rest of the script then

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.

1 comment

0

FYI: That's just a hunch, I'm not a professional scripter by any means.

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