How do I select the character?

Hello developers! Today I am making a deploying script but I get this weird error.
Reload EARLY DEVELOPMENT - Roblox Studio 7_11_2021 6_12_23 PM
My goal for the script is for it to get the character, then the HumanoidRootPart. Then it would teleport the player to a random spawn part.
Here is the function inside a script in ServerScriptService:

events:WaitForChild("DeployEvent").OnServerEvent:Connect(function(character)
	local humanoidrootpart = character:WaitForChild("HumanoidRootPart")
	local spawnparts = workspace:FindFirstChild("SpawnParts"):GetChildren()
	humanoidrootpart.CFrame = spawnparts[math.random(1,8)].CFrame
end)

And here is the localscript inside of StarterGui:

local button = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local replicatedstorage = game:GetService("ReplicatedStorage")
local events = replicatedstorage:WaitForChild("Events")
local deployevent = events:WaitForChild("DeployEvent")
button.MouseButton1Down:Connect(function()
	deployevent:FireServer(character)
end)

This is the explorer if necessary.
Reload EARLY DEVELOPMENT - Roblox Studio 7_11_2021 6_27_16 PM
Thank you all for your help!

2 Likes

the first thing in the OnServerEvent parameter is always the player that fired the remote event.

-- LOCAL SCRIPT
    local button = script.Parent
    local player = game:GetService("Players").LocalPlayer
    local character = player.Character
    if not character or not character.Parent then
       character = player.CharacterAdded:Wait()
    end
    local replicatedstorage = game:GetService("ReplicatedStorage")
    local events = replicatedstorage:WaitForChild("Events")
    local deployevent = events:WaitForChild("DeployEvent")
    button.MouseButton1Down:Connect(function()
    	deployevent:FireServer()
    end)
-- SERVER SCRIPT
    events:WaitForChild("DeployEvent").OnServerEvent:Connect(function(Player)
        local character = Player.Character
    	local humanoidrootpart = character:WaitForChild("HumanoidRootPart")
    	local spawnparts = workspace:FindFirstChild("SpawnParts"):GetChildren()
    	humanoidrootpart.CFrame = spawnparts[math.random(1,8)].CFrame
    end)
2 Likes
local humanoidrootpart = character.Character:WaitForChild("HumanoidRootPart")

or better

events:WaitForChild("DeployEvent").OnServerEvent:Connect(function(player)
	local humanoidrootpart = player.Character:WaitForChild("HumanoidRootPart")
	local spawnparts = workspace:FindFirstChild("SpawnParts"):GetChildren()
	humanoidrootpart.CFrame = spawnparts[math.random(1,8)].CFrame
end)

You can’t get the character in RemoteEvents, only the player

to use character tag is

:Connect(function(player, character)

but you can’t make that because

The LocalPlayer in a Normal Script (Remote Event OnServerEvent script) is all players

You can make only that works

local player = game.Players.LocalPlayer
1 Like

game.Players is the same as game:GetService("Players")

1 Like

Yea, is the same thing when you try use anything of Players “Folder” but is more easy to write.

1 Like

It’s just a way of how people code so…

2 Likes

Do you know how to only call one function and then it resets when the player dies?

You can use a table like local Table = {}

1 Like

If they’re in the table and they died, you can remove them from the table (Must be done in the server)
Are you going to use remote events for the function?

The function is a UserInputService function. Since my game is in third person I changed it so instead of the button, its a key press.

local label = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local replicatedstorage = game:GetService("ReplicatedStorage")
local events = replicatedstorage:WaitForChild("Events")
local deployevent = events:WaitForChild("DeployEvent")
local userinputservice = game:GetService("UserInputService")
userinputservice.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.T then
		label.Visible = false
		deployevent:FireServer()
	end
end)
1 Like

You should probably use tables in the server, When they’re deployed, add them in the table. After they die, remove them from the table.
(You can do this server-sided by using CharacterAdded & CharacterRemoving to remove them from the table.)

1 Like