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

Help with a script that makes the player unmovable?

i just a script that make the player unmovable when he gets health < 30 here is the script:(note the script is on StarterPlayerScripts) its a script like: a player will hit another player, then the player will stop moving (freeze or something like that) cuz its life will be less than 30 why it wont works?

local player = script.parent
local gui = game.Lighting.ControlScript
local humanoid = script.parent.parent.Character:FindFirstChild("Humanoid")


if humanoid.Health < 30 then
	if player:FindFirstChild("ControlScript")then
		while humanoid.Health < 30 do
	 player:FindFirstChild("ControlScript"):remove()
	    end
	end
elseif not player.PlayerScripts:FindFirstChild("ControlScript")then
	gui:Clone().Parent = player
end

i've already tried with walkspeed = 0 and anchoring player torso and head but the player can still move why??

1 comment

2

Anchor a body part of a player.

2 answers

✓ Accepted answer

Call a function when the Humanoid changes.

If the Humanoid's health is less than 30, anchor the torso.

If not, unanchor it.

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid =character:WaitForChild("Humanoid")

humanoid.Changed:connect(function()
	if humanoid.Health < 30 then
		character.Torso.Anchored = true
	else
		character.Torso.Anchored = false
	end
end)

The reason your code doesn't work is because it doesn't make sense.

If the script is in StarterPlayerScripts, this wouldn't make sense to the game because the parent of scripts in StarterPlayerScripts is PlayerScripts, which is a child of the player, since there is no Character for PlayerScripts, and in turn, no Humanoid, the code wouldn't work. Setting the player variable to script.Parent.Parent should fix your issue, however it should be common practice to make scripts involving the player LocalScripts.

Your code:

local player = script.parent
local gui = game.Lighting.ControlScript
local humanoid = script.parent.parent.Character:FindFirstChild("Humanoid")


if humanoid.Health < 30 then
    if player:FindFirstChild("ControlScript")then
        while humanoid.Health < 30 do
     player:FindFirstChild("ControlScript"):remove()
        end
    end
elseif not player.PlayerScripts:FindFirstChild("ControlScript")then
    gui:Clone().Parent = player
end

Your code, but with small changes:

local player = script.Parent.Parent
local character = player.Character
local humanoid =character:FindFirstChild("Humanoid")

humanoid.Changed:connect(function()
	if humanoid.Health < 30 then
	        while humanoid.Health < 30 do
			character.Torso.Anchored = true
	        end
	    end
	elseif humanoid.Health > 30 then
	    character.Torso.Anchored = false
	end
end)

Now you have a script that freezes the player when their health falls below 30, without rendering the character immobile.

Hope this helped you out.

2 comments

0

i put the script on the StarterPlayerScripts but the player can still move

0

The reason it didn't work is because I foolishly made a mistake by firing the function on a humanoid health change, but not a humanoid change. Look at the code now. It works.

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