how can i make a roblox script in lua that makes yourself loop follow the most close player? - lua

this just teleports me to the player not moves to them is there anyway i can fix this?
local players = game:GetService("Players")
local localPlayer = players.LocalPlayer
local closestPlayer = nil
local closestDistance = math.huge
for _, player in pairs(players:GetPlayers()) do
if player ~= localPlayer then
local distance = (player.Character.Head.Position - localPlayer.Character.Head.Position).magnitude
if distance < closestDistance then
closestDistance = distance
closestPlayer = player
end
end
end
if closestPlayer then
while true do
localPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(closestPlayer.Character.Head.Position)
wait()
end
end
im trying to get it to move to the most close player

Related

roblox studio script problems

I am making a game where when a part at the front of a car touches another part, the other part becomes unanchored. I am making a different script that gives the player a coin every time they do this. Yes, I have already made a leaderstats script. code:
function onPartTouched(otherPart)
local characterModel = otherPart.Parent
local player = game.Players:GetPlayerFromCharacter(characterModel)
if player then
local coins = player.leaderstats.coins
coins.Value = coins.Value + 1
end
end
You do actually have to attach the function lol
function onPartTouched(otherPart)
local characterModel = otherPart.Parent
local player = game.Players:GetPlayerFromCharacter(characterModel)
if player then
local coins = player.leaderstats.coins
coins.Value = coins.Value + 1
end
end
MyPart.Touched:Connect(onPartTouched)

Why is 'player' returning nil?

It keeps returning nil for player and saying that im trying to index a nil with 'WaitForChild' even though I have tried adding a wait command and changing player to 'game.Players.LocalPlayer' I'm new to scripting and I don't know what else to do.
local buyButton = script.Parent
local player = game:GetService("Players").LocalPlayer
local multiplier = player:WaitForChild("Multiplier")
buyButton.MouseButton1Up:Connect(function()
if multiplier.Value == 0 then
multiplier.Value = 1
end
end)
Instead of local player = game:GetService("Players").LocalPlayer you want to do:
local players = game:GetService("Players")
local player = players.localplayer

Roblox script error: ServerScriptService.CheckpointsScript:31: attempt to index nil with 'leaderstats'

I can not resolve this by my self.
Thank You for helping.
This is my first stackoverflow question :3
Roblox Studio information:
Latest available version as of 2022 October 03
Picture of about window:
https://i.stack.imgur.com/Wo6Br.png
https://i.stack.imgur.com/v7Jot.png
The output:
ServerScriptService.CheckpointsScript:31: attempt to index nil with 'leaderstats'
The code:
local Players = game:GetService("Players")
local CheckpointsFolder = game.Workspace.Checkpoints
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Stage = Instance.new("IntValue")
Stage.Name = "Stage"
Stage.Parent = leaderstats
--player.CharacterAdded:Connect(function(char)
-- local checkpoint = CheckpointsFolder:FindFirstChild(Stage.Value)
-- player.Character..CFrame = CFrame.new(checkpoint.PrimaryPart.CFrame.Position.Y + 5 =, checkpoint.PrimaryPart.CFrame.Rotation)
--end)
end)
for _, v in pairs(CheckpointsFolder:GetChildren()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(part)
local player = Players:GetPlayerFromCharacter(part.Parent)
local stageValue = player.leaderstats.Stage
if player and stageValue.Value < tonumber(v.Name) then
stageValue.Value = v.Name
end
end)
end
end
Your issue is coming from this line
local stageValue = player.leaderstats.Stage
And that is because the Touched event fires for any object that touches it. And you need to make sure that player actually exists.
You are doing this already, you just need to move this line inside your safety check.
local player = Players:GetPlayerFromCharacter(part.Parent)
if player then
local stage = player.leaderstats.Stage
local currentStage = tonumber(v.Name)
if stage.Value < currentStage then
stage.Value = currentStage
end
end

ServerScriptService.MainScript:88: Expected 'end' (to close 'do' at line 15), got <eof>; did you forget to close 'then' at line 82?

Help me with this please.
-- variebles define
local ReplicatedStorage = game: GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local Status = ReplicatedStorage:WaitForChild("Status")
local GameLength = 50
-- loop of the game
while true do
Status.Value = "Waiting for players (This may take a bit)"
repeat wait(1) until game.Players.NumPlayers >= 2
Status.Value = "Intermission"
wait(15)
local plrs = {}
for i, player in pairs(game.Players:GetPlayers()) do
if player then
table.insert(plrs, player) -- add de playr to de plrs table
end
end
wait(2)
local AvailableMaps = MapsFolder:GetChildren()
["Grass,Snow"]
local AvailableMaps = MapsFolder:GetChildren()
local ChosenMap = AvailableMaps[math.random(1,2)]
Status.Value = ChosenMap.Name.." Chosen"
local ClonedMap = ChosenMap:Clone()
ClonedMap.Parent = workspace
--teleport de playr
local grasstelepors = ClonedMap:FindFirstChild("grasstelepors")
if not grasstelepors then
print("no spawn m8")
end
local Availablegrasstelepors = grasstelepors:GetChildren()
["telepor"]"telepor","telepor","telepor"
for i, player in pairs(plrs) do
if player then
character = player.Character
if character then
--telepor em
character:FindFirstChild("HumanoidRootPart").CFrame = Availablegrasstelepors[1]
table.remove(Availablegrasstelepors,1)
--give them swords so they can violence
local Sword = ServerStorage.Sword:Clone()
Sword.Parent = player.Backpack
local GameTag = Instance.new("BoolValue")
GameTag.Name = "GameTag"
GameTag.Parent = player.Character
else
--no charecter m8
if not player then
table.remove(plrs,i)
end
end
end
end
The error is telling you everything.
The code expected there to be an end to close your while true do on line 15, but instead of finding it, it hit the end of the file.
It is even suggesting where you made the mistake, "did you forget to close your if player then statement on line 82?"
Add an end at the end of the code block to fix it.

Set Humanoid WalkSpeed

I found this script in a similar question but, I'm getting the error 16:11:18.560 - Workspace.Script:2: attempt to index nil with 'Character'
local Player = game:GetService("Players").LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
-- setting speed
local Humanoid = character:WaitForChild("Humanoid")
if Humanoid then
Humanoid.WalkSpeed = 25
end
Can anyone help me?
The LocalPlayer object only exists in LocalScripts. That's why your variable Player is nil.
There are two ways you can fix this :
1) Move this code into a LocalScript, or
2) Add this code to a callback that is executed when a player joins the game. Here's what that would look like.
local PlayerService = game:GetService("Players")
-- wait for a player to join the game
PlayerService.PlayerAdded:Connect( function(Player)
-- wait for the player's character to load
Player.CharacterAdded:Connect( function(Character)
-- set the speed
local Humanoid = Character:WaitForChild("Humanoid")
if Humanoid then
Humanoid.WalkSpeed = 25
end
end)
end)
Or make a brick that gives you speed:
local KillBrick = script.Parent()
KillBrick.Touched(function(kill)
local humanoid = kill.Parent:FindFirstChild("humanoid")
if humanoid then
humanoid.WalkSpeed = 25
end
end)

Resources