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.
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
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
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.
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
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()
mouse.KeyDown:connect(function(key)
key = key:lower()
if key == "e" then
ChangeCharacter(char)
char = script.Parent.Parent:WaitForChild("Character")
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.
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
local Ball = current.Ball
Ball.Parent = workspace
local move = Instance.new("RocketPropulsion", Ball)
move.Target = new["Left Arm"]
move.TargetRadius = 1
move.MaxSpeed = 30
move:Fire()
move.ReachedTarget:connect(function()
move:Abort()
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()
mouse.KeyDown:connect(function(key)
key = key:lower()
if key == "e" then
ChangeCharacter(char)
char = script.Parent.Parent:WaitForChild("Character")
end
end)
Hope this helps with the understanding. Good luck with the game!