Need help with accesing a players character

A little explanation:
I am currently working in a turn based game, i have gotten nothing done to be fair, i’m new to scripting and im having trouble finding a player’s model.

I’ve been stuck on this for three days, i seriously need help, i’ve tried to make a variable of the player name and acces the model trough that but it doesn’t work

im kind of desperate and i couldn’t find a tutorial that would help.

The problem is I need to lock the player in place once they enter in contact with the enemy (starting the fight) i’m currently just testing the locking the player down.

Problem is i can’t seem to acces the humanoid part to set the walkspeed to 0

please if you know anything that could help, tell me

3 Likes

If you have a reference to the actual player instance you can use its Character property. That gives you a reference to their character model in Workspace.

Alternatively you can get the character from the Player.CharacterAdded event.

I’m currently using the CharacterAdded event, then create a variable of the players name, so i could re use it but it doesnt seem to work

If you already have the player name, then this is easy. What you could do is:
local Players = game:GetService("Players")
local PlayerName = "playernamehere"
local PlayerChar = Players[PlayerName].Character or Players[PlayerName].CharacterAdded:Wait()

Basically what that does is it grabs the Players Character, but if its nil then we wait for it to be added by using the :Wait() method for RBXScriptSignal. This also saves it into a variable, so you could easily do PlayerChar.Humanoid.WalkSpeed = 0

I will try this, I understand what it does and im sure i can make it work with what i currently have, thanks!

No problem, also for other threads you will make in Scripting Support, please add your current code that is having the issue. It will help you and people trying to debug your code.

Is there an easy way to get the player name? I feel like what im currently using doesn’t work

Could you please add your current code to the thread? I would be able to give you feedback if you do.

player.Name is the easiest way.

2 Likes

This is what i currently have, hope I didn’t duck something up

local player = game.Players.LocalPlayer

local function onCharacterAdded(character)

PlayerName = character.Name

Players = game:GetService(“Players”)

PlayerChar = Players.PlayerName.CharacterAdded:Wait()

end

local function OnTouch(hit)

print(“Teleport”)

local human = hit.Parent:findFirstChild(“Torso”)

if (human ~=nil) then

human.CFrame= CFrame.new(Vector3.new(-23.8, 0.6, -39.2))

PlayerChar.Humanoid.WalkSpeed = 0

print (PlayerName … " has started a fight with a bandit")

end

end

script.Parent.Touched:connect(OnTouch)

game.Players.PlayerAdded:Connect(onCharacterAdded)

I have to add, when i print the player name it works, but it doesn’t lock in place…

Alright, so you really dont need to do what anyone told you now. It’s a bit funny, but you had the right idea. So onto the code part, you can actually get the character from your OnTouch function.

local function OnTouch(hit)

print(“Teleport”)

local torso = hit.Parent:findFirstChild(“Torso”)
local theirchar = nil

if (torso ~= nil) then
theirchar = torso.Parent
torso.CFrame= CFrame.new(Vector3.new(-23.8, 0.6, -39.2))

theirchar.Humanoid.WalkSpeed = 0

print (theirchar.Name .. " has started a fight with a bandit")

end

end

script.Parent.Touched:Connect(OnTouch)

Remove the onCharacterAdded code now, you dont need it, also make sure to remove
the player added line too.

EDIT:
fixed code to work now lol

2 Likes

Thanks a lot, I didn’t post it initially because i tought people would laugh at what i had done, its obvious i have no idea what im doing, thanks for the help

If this is a server script then you can’t use LocalPlayer

Nope, no one does that here. Like at all. We all are grown up enough not to make fun of eachother, sure we might have a little giggle but thats respectful. Make sure to mark my post as a solution if it works.

2 Likes