Getting a nil response while trying to clone a object to backpack - lua

I am trying to clone an object from the replicatedstorage to the players backpack when an object part is touched and the code looks fine for me but it keeps giving a nil response from clone.parent = player.backpack
local replicatedtorage = game:GetService("ReplicatedStorage")
local Sword = replicatedtorage:FindFirstChild("Sword")
local part = game.Workspace.Part
local player = game.Players.LocalPlayer
local clone = Sword:Clone()
part.Touched:Connect(function(hit)
local humanoid = hit.parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
clone.Parent = player.Backpack
end
end)

This looks like a server Script which cannot access Players.LocalPlayer like clients can because there is no local player to the server. A way to get the Player that touched a part is through Players:GetPlayerFromCharacter() which requires one instance to be passed and will either return the Player whose Character is that instance or nil.
part.Touched:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player then
clone.Parent = player.Backpack
end
end)
This should work right away in your script and can replace your existing Touched connection.

Related

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

"attepted to index nil with Humanoid"

local animationstomp = script.Animation
local user = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character
local human = char.Humanoid
user.InputBegan:Connect(function(input, proccesedevent)
if input.KeyCode == Enum.KeyCode.F then
local animationtrack = human:LoadAnimation(animationstomp)
animationtrack:play()
end
end
end)
this script keeps returning error "attempted to index nil with huamnoid" i have looked for solutions on dev forum and on stack overflow but i could not find anything so im going to post myself. anyone catch any erros?
"attempted to index nil with huamnoid"
What that means is that this script loaded before the player's character loaded in.
To fix this, you can use
local char = player.Character or player.CharacterAdded:Wait()
-- uses the player if it's already loaded in, or waits for the character to load in
Also make sure to use
local human = char:WaitForChild("Humanoid")
-- to make sure you wait till the humanoid gets added
Final script
local animationstomp = script.Animation
local user = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
user.InputBegan:Connect(function(input, proccesedevent)
if input.KeyCode == Enum.KeyCode.F then
local animationtrack = human:LoadAnimation(animationstomp)
animationtrack:play()
end
end)

Attempt to call a nil value when changing an IntValue for leaderstats

I'm trying to make an admin panel, everytime I want to change the value with a username
Code:
leaderstats Script;
--// Set up folder
local AdminModule = require(game:GetService('ServerScriptService').leaderstats.MainModule)
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new('Folder', plr)
leaderstats.Name = 'leaderstats'
local Playtime = Instance.new('IntValue', leaderstats)
Playtime.Name = 'Playtime'
end)
AdminModule.GivePoints('happy_speler', 500)
MainModule:
local module = {
GivePoints = function(plr, amount)
plr:WaitForChild('leaderstats'):WaitForChild('Playtime').Value = amount
end,
}
return module
The error is telling you that you have tried to call a function that doesn't exist.
Looking at the AdminModule.GivePoints function, it looks like it expects a Player object for the plr argument, but you have passed in a string. The string library does not have a WaitForChild function, so calling plr:WaitForChild is throwing the error.
The way to fix this is to properly pass in a Player object :
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local AdminModule = require(ServerScriptService.leaderstats.MainModule)
Players.PlayerAdded:Connect( function(plr)
local leaderstats = Instance.new('Folder', plr)
leaderstats.Name = 'leaderstats'
local Playtime = Instance.new('IntValue', leaderstats)
Playtime.Name = 'Playtime'
if plr.Name == 'happy_speler' then
AdminModule.GivePoints(plr, 500)
end
end)

attempt to index nil with 'LoadCharacter'

When trying to respawn the player with plr:LoadCharacter() it just gives me:
attempt to index nil with 'LoadCharacter' & I tried multiple ways to do it such as Player:LoadCharacter() or is there a more efficient way to kill/respawn the player?
--Declared Boolean Global variable
_G.TimerStart = false
--Local Paths to Objeccts
local label = game.StarterGui.TimerGUI.Timer
-- Get Service Variables
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer
--Get Children
local Child = game.Players:GetChildren()
-- Wait for Child Variables
local TimeCountdown = ReplicatedStorage:WaitForChild("Timer")
local timerevent = ReplicatedStorage:WaitForChild("Timer")
local function Thieves(Players)
if _G.TimerStart == false then
for i,v in pairs(game.Teams.Thieves:GetPlayers()) do
game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
wait(2)
plr:LoadCharacter()
wait(7)
timerAmount = 120
end
for i,v in pairs(game.Teams.Police:GetPlayers()) do
game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
wait(2)
plr:LoadCharacter()
wait(7)
timerAmount = 120
end
end
end
In this case plr is a nil value. So plr:LoadCharacter() is not allowed as it does not make any sense.
local plr = game:GetService("Players").LocalPlayer
is the reason.
So refer to this manual page: https://developer.roblox.com/en-us/api-reference/property/Players/LocalPlayer
Maybe this helps:
Loading GUIs When creating loading GUIs using ReplicatedFirst, sometimes a LocalScript can run before the LocalPlayer is available.
In this case, you should yield until it becomes available by using
Instance:GetPropertyChangedSignal
local Players = game:GetService("Players")
-- Below: access Players.LocalPlayer; if it is nil, we'll wait for it using GetPropertyChangedSignal.
local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()

Can't clone sword into player backpack

Im trying to clone a sword from replicated storage into all players who are in the round's backpack backpack.
code:
for i, v in pairs(game.Workspace.PlayerInRound:GetChildren()) do
local sword = game.ReplicatedStorage.ClassicSword:Clone()
sword.Parent = game.Players.v.Backpack
end
but it just says v is not part of players.
sword.Parent = game.Players.v.Backpack is the same as sword.Parent = game.Players['v'].Backpack, which is definitely senseless. Since v is an object, perhaps, you need sword.Parent = v.Backpack.
What script are you using? If local script, do this.
local player = game.Players.LocalPlayer
local workspaceplayer = game.Workspace:WaitForChild(player.Name)
Sword.Parent = workspaceplayer.BackPack
The workspaceplayer means the player in the workspace.
Script
game.Players.PlayerAdded:Connect(function(player)
Sword.Parent = game.Workspace:WaitForChild(player.Name).BackPack
end)

Resources