How do you create a light inside the player? - roblox

Im trying to create a light when the player spawns in but im running into multiple problems
I've tried many formats and functions always keep getting either
"Attempt to index nil value"
"[head(and other things)] is not a member of [humanoid(and other things)]"
game.Players.PlayerAdded:Connect(function(playersdude)
playersdude.CharacterAdded:Connect(function(char)
local humanoid = char:WaitForChild("Humanoid")
local light = Instance.new("PointLight")
light.Parent = game.Players.LocalPlayer.HumanoidRootPart
end)
end)

You're running into the same problem as this guy : (attempt to index field 'LocalPlayer' (a nil value))
I'm assuming that you've written this in a Script somewhere. LocalPlayercan only be accessed in a LocalScript. Trying to access it from a server Script will result in LocalPlayer being nil. Luckily, you don't need to use LocalPlayer at all!
You can use the char provided in the CharacterAdded connection to find the player's head.
game.Players.PlayerAdded:Connect(function(playersdude)
playersdude.CharacterAdded:Connect(function(char)
-- search through the character model to find the head
local head = char:FindFirstChild("Head", true)
-- add a light bright enough to make them glow like the mid-morning sun
local light = Instance.new("PointLight", head)
light.Brightness = 100
end)
end)

Related

How do I change leaderstats value on roblox?

I'm trying to test out a game that gives coins to the player(via leaderstats) when you touch this block. When I touch the block though, my leaderstats don't change and I don't get any errors. Heres my Script
enter image description here
game.Players is a service and is not the Player who owns the Character that touched TestPart. The Players service provides a method to get the Player from a Character called GetPlayerFromCharacter(character).
TestPart.Touched:Connect(function(hit)
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
plr.leaderstats.Coins.Value += 1
end
end)
Luau supports increments (+=) but not lua.

Can i use an if statement for a player name? [ROBLOX]

I am on Roblox again, and I wanna make a script where when a part is touched, if the player name is lorenzoomh, set the brickcolor to green. But if not, set it to red. my current code:
script.Parent.Touched:Connect(function(igottouched)
if game.Players.LocalPlayer.Name == "lorenzoomh" then
script.Parent.BrickColor = BrickColor.Green()
else
script.Parent.BrickColor = BrickColor.Red()
end
end)
which doesnt work btw
The answer to your question, "can I use a player's name in an if-statement" is yes. Your problem is that the LocalPlayer doesn't exist where you're trying to use it from.
See the docs for Players.LocalPlayer :
This property is only defined for LocalScripts ... as they run on the client. For the server (on which Script objects run their code), this property is nil.
You need to access the Player object a different way. The Touched event on a Part gives you access to the other object that touched it. You can use that to check if the other part belongs to a player's character model.
local target = game.Workspace.Part
-- listen for a player touching a block
target.Touched:Connect( function(otherPart)
-- check that the thing that touched is a character model
local character = otherPart.Parent
local isCharacter = character:FindFirstChild("Humanoid") ~= null
if isCharacter then
-- the character model name is usually the name of the player so use that
if character.Name == "lorenzoomh" then
target.BrickColor = BrickColor.Green()
else
target.BrickColor = BrickColor.Red()
end
end
end)
The Players service has a method for correlating a character model with a Player object specifically if a simple name check is not sufficient for your use case.

How can you load the player character when they join?

I want to make a Roblox game with multiple scenes that you can load your character into.
The problem with this is that I cannot find any way to do this. I’ve done a reasonable amount of research online, but all I get are guides on how to load a character once using a plugin, rather than getting the person who joined’s character and loading it into a certain position.
How to get the person who joined’s character and load it into a certain position?
To create somebody's character, call Players:CreateHumanoidModelFromUserId with the Player's UserId. Then you can move it by calling PivotTo on their HumanoidRootPart.
Note: do not use Model.PrimaryPart to grab the HumanoidRootPart. On R6 characters, this is actually the character's Head. You're better off calling FindFirstChild("HumanoidRootPart") (or WaitForChild).
You'll notice that this positions the character from their torso's center, and not from their feet. So you'll need to offset that vertically by 3 studs if the character is not Rthro (is of fixed size, like R6). If the character is Rthro (is R15, and your game has scaling enabled), then you'll need to call GetExtentsSize on the character Model.
So, something like:
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local function loadStage(player: Player)
-- ...
local character = Players:CreateHumanoidModelFromUserId(player.UserId)
local rootPart = character:FindFirstChild("HumanoidRootPart")
-- assuming R6 this will be fine
rootPart:PivotTo(CFrame.new(myDesiredPosition + Vector3.yAxis * 3))
rootPart.Parent = Workspace
-- ...
end
Players.PlayerAdded:Connect(function(player)
loadStage(player)
end)

Roblox - attempt to call a nil value

When trying to create a code, that on FireServer with Specific Player chosen a GUI would've been cloned into their PlayerGui, But instead I get a nil value.
Code:
local Blinder = game.ReplicatedStorage.RCD.Blind
Blinder.OnServerEvent:Connect(function(player, PlayerToBlind)
if not player:IsInGroup(7465879) then return false end;
script.BL:Clone().Parent = PlayerToBlind:WaitForChild("PlayerGui")
print("Done")
end)
Basically what I try to reach is if my Admin Panel Remoteevent is fired, and a Target has been chosen, the Targeted Player will become a Cloned GUI into their PlayerGui
Any fix on this error?
The error message itself is telling you that you are calling a function that doesn't exist. This issue is caused by an object not being the type you expect.
Unfortunately, the message is pointing at a line that has a few function calls, so it's difficult to say what is causing the exact error. Either script.BL isn't an object with a Clone() function, or PlayerToBlind isn't an object with a WaitForChild() function.
If you can break the operations into a few different lines and add safety checks along the way, you can guarantee that your code is safe and being called properly.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Blinder = ReplicatedStorage.RCD.Blind
local BL = script.BL
Blinder.OnServerEvent:Connect(function(player, PlayerToBlind)
-- check that the person calling Blinder:FireServer actually provided a PlayerToBlind
assert(type(PlayerToBind) == "string", "Blinder expects PlayerToBind to be the name of a player")
-- escape if the player is not in the appropriate group
if not player:IsInGroup(7465879) then
return
end
-- find the Player object based on the name provided from PlayerToBlind
local blindedPlayer = Players:FindFirstChild(PlayerToBlind)
if not blindedPlayer then
warn("Could not find player named : ", PlayerToBlind)
return
end
-- create a blinding UI on the other player
local targetGui = blindedPlayer:WaitForChild("PlayerGui")
local newBL = BL:Clone()
newBL.Parent = targetGui
print("Done")
end)
By adding asserts into your code, it won't stop your code from breaking, in fact it will force your code to break faster and in expected ways.
The issue has been fixed,
I have been setting up the LocalScript wrong.
(I grabbed the Text from a TextButton, Instead of the TextBox)

Roblox - while testing trusses work when i drag them in, but when i use script to set all the stuff, then i cant climb it

I am using a script to set cancollide = true and transparency = 0, but i cant climb the truss. but when I'm already in testing mode (in studio) and i drag in the same truss, i can climb it, and i am looking at properties, its same, both anchored is true, they are touching same parts, i dont know why this is happening. Please help thanks :)by the way, i am making tycoon and this is script i am using:
wait(1)
amount = 0 -- cost of model
owner = script.Parent.Parent.Owner
local stun = false
pcall(script.Parent.Head.Touched:connect(function(hit)
if hit.Parent ~= nil then
player = game.Players:findFirstChild(hit.Parent.Name)
if not stun and player ~= nil then
if player.Name == owner.Value then
if player:findFirstChild("leaderstats") ~= nil then
stats = player:findFirstChild("leaderstats")
if stats.Money.Value >= amount then
stun = true
stats.Money.Value = stats.Money.Value - amount
script.Parent.ladder.CanCollide = true
script.Parent.ladder.Transparency = (0)
script.Parent.Head:Remove()
wait(1)
stun = false
end
end
end
end
end
end))
dont worry about other stuff, it works, its just this part that matters now:
script.Parent.ladder.CanCollide = true
script.Parent.ladder.Transparency = (0)
script.Parent.Head:Remove()
Please help :( this is the problem with the ladder using script not working and same dragged in from toolbox truss working, iv done this with many trusses and ladders and same result :(
So, a error I noticed in your code, is that you erase your Character's Head, which would kill your character, or possibly throw a error if the .Touched event fires two times.
There are many efficency errors in this script, such as .Transparency = (0), or doing script.Parent.ladder instead of using variables, but it really is no problem most of the times. Something you could try, is to use Instance.new() to create the ladder.If this game is FilteringEnabled on (Expermiental mode off), please note your script won't work as intended if it is a LocalScript, it will only work well on Server-Side (aka, a normal Script instead of a LocalScript)