Hello developers! Today I am making a deploying script but I get this weird error.
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.
Thank you all for your help!
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)
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)
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.)