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

How do you make the Move and Change Character Script? Read the rest please.

I need help for my game called NBA GM Mode... It's like MyTeam Mode in NBA2k16... But in Roblox. It's like when you press E, It will change to another person/character but it will only change if the character that you're in doesn't have the ball...And if the character you're in when playing has the ball, it'll pass the ball to the person/character you're looking at or near... If you understand please help. So... How do you make a script that when you press E, it will change to another NPC or if you have the ball and you press E it will pass it to the nearest character? By the way, just tell me like 10 of the objects needed like FindFirstChild. No need to tell of the code... I know it says "Explain what you've tried so far. Don't come into the picture just asking for someone to do all of the work for you.

Provide an example of the code you have tried, and explain what you want it to do." I don't know what to do after the GUIs I did... I didn't know what to do... I didn't want to abandon my project... Can you help me?

1 answer

I'd have to see how your game works to explain you all, but the things I can tell you are the basics for that. I'll explain all with scripts, comments and a bit of text.

--This script is in PlayerGui
char = script.Parent.Parent:WaitForChild("Character")
allplayers = workspace:WaitForChild("Players"):GetChildren() -- Players is gonna be a folder in workspace that
--contains all the characters
function ChangeCharacter(current)
	-- Now we're gonna start by creating the function. The current variable is gonna be the character
	-- before the changing.
	local new = allplayers[math.random(1, #allplayers)]
	if current.HasBall.Value == true then -- The HasBall value will show whether the player has the ball.
		current.HasBall.Value = false
		new.HasBall.Value = true -- Changed who has the ball
		local Ball = current.Ball
		Ball.Parent = workspace
		Ball.Parent = new -- Changed the ball's parent, so the values aren't faked
		script.Parent.Parent.Character = new -- Changing the player's character
	elseif current.HasBall.Value == false then
		script.Parent.Parent.Character = new
	end
end

Now that's some pretty raw piece of code that's just used as example. It is a function to change the character AND change the ball's parent IF the player is currently having the ball. But now this would just be a function that's not being called, so we have to add it to an event.

--This script is in PlayerGui
char = script.Parent.Parent:WaitForChild("Character")
allplayers = workspace:WaitForChild("Players"):GetChildren()
function ChangeCharacter(current)
	local new = allplayers[math.random(1, #allplayers)]
	if current.HasBall.Value == true then
		current.HasBall.Value = false
		new.HasBall.Value = true -- Changed who has the ball
		local Ball = current.Ball
		Ball.Parent = workspace
		Ball.Parent = new
		script.Parent.Parent.Character = new
	elseif current.HasBall.Value == false then
		script.Parent.Parent.Character = new
	end
end

mouse = script.Parent.Parent:GetMouse() -- Returns the player's mouse and can also be used for the
-- keyboard

mouse.KeyDown:connect(function(key) -- That's an event that's being called whenever a player hits 
-- a key
	key = key:lower() -- We could also use :upper(); it's just to make the code smaller.
	if key == "e" then -- If the lowered key is e 
		ChangeCharacter(char) -- Calling the function
		char = script.Parent.Parent:WaitForChild("Character") -- Redefining the char variable
	end
end)

Now we can add movement to the ball. The best for this would be a RocketPropulsion, I guess. So let's add this.

--This script is in PlayerGui
char = script.Parent.Parent:WaitForChild("Character")
allplayers = workspace:WaitForChild("Players"):GetChildren()
function ChangeCharacter(current)
	local new = allplayers[math.random(1, #allplayers)]
	if current.HasBall.Value == true then
		current.HasBall.Value = false
		new.HasBall.Value = true -- Changed who has the ball
		local Ball = current.Ball
		Ball.Parent = workspace
		local move = Instance.new("RocketPropulsion", Ball)
		move.Target = new["Left Arm"] -- You may as well do the Right Arm.
		move.TargetRadius = 1
		move.MaxSpeed = 30
		move:Fire() -- Starts the moving
		move.ReachedTarget:connect(function() -- That's the event that's being called when the Ball reaches the TargetRadius.
			move:Abort() -- Stops the moving
			move:Destroy()
			Ball.Parent = new
			script.Parent.Parent.Character = new
		end) 
	elseif current.HasBall.Value == false then
		script.Parent.Parent.Character = new
	end
end

mouse = script.Parent.Parent:GetMouse() -- Returns the player's mouse and can also be used for the
-- keyboard

mouse.KeyDown:connect(function(key) -- That's an event that's being called whenever a player hits 
-- a key
	key = key:lower() -- We could also use :upper(); it's just to make the code smaller.
	if key == "e" then -- If the lowered key is e 
		ChangeCharacter(char) -- Calling the function
		char = script.Parent.Parent:WaitForChild("Character") -- Redefining the char variable
	end
end)

Hope this helps with the understanding. Good luck with the game!

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