GetPlayerFromCharacter won't switch team

  1. What do you want to achieve?

The player who died from the lava to be switched to the “lobby” team

local TweenService = game:GetService("TweenService")
local part = script.Parent
local repStorage = game:WaitForChild("ReplicatedStorage")
local goal = {}
goal.Position = Vector3.new(part.Position.X, 300, part.Position.Z)

local function getPlayerFromCharacter(character)
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character == character then
			return player
		end
	end
end

local tweenInfo = TweenInfo.new(50, Enum.EasingStyle.Linear,Enum.EasingDirection.InOut)
local tween = TweenService:Create(part, tweenInfo, goal)
function lavaRise ()
	if repStorage.startLavaRise.Value == true then
		tween:Play()
	end
end
script.Parent.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("HumanoidRootPart") ~= nil then 
		part.Parent.Humanoid.Health = 0
		getPlayerFromCharacter(part.parent)
		player.Team= game:GetService("Teams").Lobby
	end
end)
repStorage.startLavaRise.Changed:connect(lavaRise)



1 Like

You never reference the player variable, try this:

script.Parent.Touched:Connect(function(part)
	if part.Parent:FindFirstChild("HumanoidRootPart") ~= nil then 
		part.Parent.Humanoid.Health = 0
		local player = getPlayerFromCharacter(part.parent)
        print(player)

        if player then
		    player.Team= game:GetService("Teams").Lobby
        end
	end
end)

local player = game.Players.LocalPlayer

If this is a ServerScript, you can’t reference the Player that way as that’ll only work in LocalScripts

Instead you have to think of a wrap-around by either using the GetPlayerFromCharacter function, or referencing it inside the workspace

you can use a touch event and detect if the player has touched the part and u can use game.Players:GetPlayerFromCharacter(hit.Parent)

Heres a kinda fixed script I made, the problem is you never made a reference of the “player” variable. (I fixed the script on a iPad so pardon me for any mistakes)

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local part = script.Parent
local repStorage = game:WaitForChild("ReplicatedStorage")
local goal = {}
goal.Position = Vector3.new(part.Position.X, 300, part.Position.Z)

local tweenInfo = TweenInfo.new(50, Enum.EasingStyle.Linear,Enum.EasingDirection.InOut)
local tween = TweenService:Create(part, tweenInfo, goal)
function lavaRise()
	if repStorage.startLavaRise.Value == true then
		tween:Play()
	end
end

script.Parent.Touched:Connect(function(part)
    local player = Players:GetPlayerFromCharacter(part.Parent)
	if player.Character then 
		player.Character:FindFirstChildWhichIsA("Humanoid").Health = 0
		player.Team = game:GetService("Teams").Lobby
	end
end)

repStorage.startLavaRise:GetPropertyChangedSignal("Value"):Connect(lavaRise)
1 Like

You mentioned part twice, one as a local variable and another in the touched function. Maybe change script.Parent.Touched:Connect(function(part) to part.Touched:Connect(function(hit)

Another thing is that you never referenced getPlayerFromCharacter as a variable. To fix that, just do local player = Players:GetPlayersFromCharacter(hit.Parent). After that, add an if statement if the player that has died exists if player then
Here is your full code:

part.Touched:Connect(function(hit)
   if hit.Parent:FindFirstChild("HumanoidRootPart") ~= nil then
       hit.Parent.Humanoid.Health = 0
       local player = Players:GetPlayerFromCharacter(hit.Parent)
       if player then -- If touched part and player exists
           player.Team = game:GetService("Teams").Lobby
        end
    end
end)

Also, next time, please explain more about your issue that you need help with, eg. if so, share errors from the output etc. Hopefully this helped

1 Like

I did not realise the “part” variable exists, but well

1 Like