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

How do I reference a player's character model to edit humanoid properties with a local script?

I'm trying to use a local script that uses a bool value in my Player's stats (created with a data store) that determines if they are in battle or not. The script sets the humanoid properties WalkSpeed and JumpPower to 0.

local Fighting = script.Parent.Parent:WaitForChild("PlayerStats").InBattle -- Value determining whether they are in battle

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character.Humanoid -- I've also set to character:WaitForChild("Humanoid"), I believe there's no difference, and I don't see one.
		while true do
			if Fighting.Value == true then -- if they are fighting, they can't move
				humanoid.WalkSpeed = 0
				humanoid.JumpPower = 0
			else
				humanoid.WalkSpeed = 16
				humanoid.JumpPower = 50
			end
			wait(0.01)
		end		
	end)
end)

When I run the game and set the InBattle.Value to true, nothing happens and no errors occur. I also tried running a server which didn't work. I believe I'm having trouble referencing the humanoid object to edit with my code.

1 answer

✓ Accepted answer

Here I understand what your intention was, but there is no way to check if it is true or false, it is not the same as a part where you can check if CanCollide is true or false. Here you would have to check if the BoolValue is in the folder or not .

while true do
            if Fighting.Value == true then -- if they are fighting, they can't move
                humanoid.WalkSpeed = 0
                humanoid.JumpPower = 0

Here is the script I made, if you have any questions ask, if it is within my power I will answer them

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
repeat wait() until char:FindFirstChild("Humanoid")
local human = char.Humanoid

local playerstat = Instance.new("Folder")
playerstat.Name = "PlayerStats"				--Here I created the folder in the same script, but you can remove it if you have already made it in another script 
playerstat.Parent = plr

while wait(2) do

local Fighting = plr:WaitForChild("PlayerStats")

	local inbat = Fighting:FindFirstChild("InBattle") -- You have to see if the BoolValue is in your "PlayerStats" folder

	if inbat then --If he is then 
		human.WalkSpeed = 0
		human.JumpPower = 0
	else -- if not
		human.WalkSpeed = 16
		human.JumpPower = 50
	end
	
	end	

2 comments

0

Thank you! It worked, I just needed to add inbat.Value. I appreciate the help!

0

I added inbat.Value to the if statement by the way.

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