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 make the snake go faster than the player?

I'm creating a script which summons a snake. The snake however is NOT allowed to follow the owner, but it will follow anyone within a 40 stud radius. The script runs perfectly fine.

However, I want the snake to move just a little bit faster than the player so the snake touches the player and damages them. (Damage is done in a separate script). Any ideas how I would come about making the snake faster?

valuee = script.Parent.PlayersName.Value

repeat wait() until game.Players.ChildAdded
wait()
while true do wait()
    for _, v in pairs(game.Players:GetChildren()) do
        local plr = v.Character
        if plr.Humanoid.Health > 0 and plr.Name ~= script.Parent.PlayersName.Value and plr.Name ~= "Snake" then
            local head = plr.Head
            if math.abs((head.Position-script.Parent.Head.Position).Magnitude) <= 40 then
                script.Parent.Head.BodyPosition.Position = head.Position
						elseif plr.Humanoid.Health > 0 and plr.Name == script.Parent.PlayersName.Value or plr.Name == "Snake" then
	
	wait(0.1)
	
	
			
            end
        end
    end
end




Thank you.

3 answers

✓ Accepted answer

You're using a BodyPosition object, so you should be able to mess with D Determines the amount of dampening to use in reaching the goal and P Determines how aggressive of a force is applied in reaching the goal position

local snakemodel = script.Parent
local snakehead = snakemodel:WaitForChild('Head')
local snakebodypos = snakehead:WaitForChild('BodyPosition')
local snakeowner = snakemodel:WaitForChild("PlayersName")
-- Use WaitForChild()
-- Use local variables, not global. 
local players = game:GetService('Players')
-- Index players now
players.ChildAdded:wait()

snakebodypos.D = 1250
snakebodypos.P = 10000
-- These are the default BodyPosition Values

while true do 
	for _, v in pairs(players:GetPlayers()) do
		-- Use GetPlayers() when you're accessing Players
		local character = v.Character
		local plr = game.Players:GetPlayerFromCharacter(character)
		local hum = character:findFirstChild("Humanoid")
		if hum and hum.Health > 0 then 
			if plr.Name ~= snakeowner.Value then
				local plrhead = character:findFirstChild("Head")
				-- Use findFirstChild() on something that may error.
				if plrhead then 
					local dist = (plrhead.Position-snakehead.Position).magnitude <= 40 
					if dist then 
						snakebodypos.Position = plrhead.Position
					else
						wait(.1)
					end
				end
			end
		end
	end
	wait()
end

1 comment

1

Thank you for explaining the different components and thanks for answering!

So you don't necessarily need to use a script for this, since you already have the snake model with the bodyPosition in it, all you need to do is increase the P value to around 20,000 if you want it to outrun the player at default walkspeed.

1 comment

1

Legend. Wish I knew this before hand. Thank you!

Well, considering the snake has a humanoid, it is actually quite easy. You just change the Walkspeed of the snake's humanoid. I am NOT promising this will work, so don't downvote.

valuee = script.Parent.PlayersName.Value

repeat wait() until game.Players.ChildAdded
wait()
while true do wait()
    for _, v in pairs(game.Players:GetChildren()) do
        local plr = v.Character
        if plr.Humanoid.Health > 0 and plr.Name ~= script.Parent.PlayersName.Value and plr.Name ~= "Snake" then
            local head = plr.Head
            if math.abs((head.Position-script.Parent.Head.Position).Magnitude) <= 40 then
                script.Parent.Head.BodyPosition.Position = head.Position
                        elseif plr.Humanoid.Health > 0 and plr.Name == script.Parent.PlayersName.Value or plr.Name == "Snake" then

    wait(0.1)
    plr.Humanoid.Walkspeed = "Insert Value Here"


            end
        end
    end
end

I hope this works for you.

5 comments

0

snake needs legs in order to walk

0

^ Walkspeed only works with Rigs. The snake is one part only. Thanks for your suggestion though.

0

I am not sure if walkspeed requires legs to work, and snakes dont have legs. Well why don't you just add a rig? Plus real snakes have joints.

0

Because i'd prefer to keep it plain and simple.

0

nice idea

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