roblox Cframe is not a valid member of head - touch

For some reason, I get the error " Cframe is not a valid member of MeshPart "Workspace.[myuser].Head"
local find = script.Parent
find.Touched:Connect(function(hit)
local de = hit.Parent:FindFirstChild("Humanoid")
if de ~= nil then
local head = hit.Parent.Head
find.CFrame = head.Cframe * CFrame.new(0, 10, 0)
local poop = Instance.new("Weld", find)
poop.Part0 = head
poop.Part1 = find
end
end)
could someone help quickly?

Related

ServerScriptService.Size:4: attempt to index nil with 'Humanoid'

Cant figure out what the problem is
while wait(.5) do
local children = game.Players:GetChildren("Humanoid")
for i = 1, #children do
local hum = children[i] .Character.Humanoid
hum.HeadScale.Value = children[i].leaderstats.Points.Value/50 +1
hum.BodyHeightScale.Value = children[i].leaderstats.Points.Value/50 +1
hum.BodyWidthScale.Value = children[i].leaderstats.Points.Value/50 +1
hum.BodyDepthScale.Value = children[i].leaderstats.Points.Value/50 +1
hum.WalkSpeed = children[i].leaderstats.Points.Value/3.12
hum.JumpPower = children[i].leaderstats.Points.Value
hum.MaxHealth = children[i].leaderstats.Points.Value+25
end
end
was trying to get a simple size script to work.
The attempt to index nil with Humanoid error is telling you that the object that you are trying to grab the Humanoid out of doesn't exist. This line specifically :
local hum = children[i].Character.Humanoid
When a player joins the game, there is an indeterminate amount of time before their character model actually spawns into the world. So between when they join and when their character spawns, the Player.Character property is nil.
Your code does not handle the possibility that the character model is nil, and you are trying to access the humanoid immediately.
A safer approach is not to use a loop, but to observe changes to the leaderstats values and react to those changes. So in your code that creates your leaderstats objects, do something like this :
local Players = game:GetService("Players")
local function updateCharacter(hum : Humanoid, points : number)
local bodyScale = (points / 50) + 1
local walkSpeed = points / 3.12
local jump = points
local health = points + 25
hum.HeadScale.Value = bodyScale
hum.BodyHeightScale.Value = bodyScale
hum.BodyWidthScale.Value = bodyScale
hum.BodyDepthScale.Value = bodyScale
hum.WalkSpeed = walkSpeed
hum.JumpPower = jump
hum.MaxHealth = health
end
Players.PlayerAdded:Connect(function(player)
-- when a player joins the game, give them a leaderstats folder
local leaderstatsFolder = Instance.new("Folder")
leaderstatsFolder.Name = "leaderstats"
local pointsValue = Instance.new("IntValue", leaderstats)
pointsValue.Name = "Points"
pointsValue.Value = 0 -- or load from dataStores
leaderstatsFolder.Parent = player
-- listen for changes to the points and resize the character
pointsValue.Changed:Connect(function(newValue)
-- escape if the character doesn't exist
local character = player.Character
if character then
updateCharacter(character.Humanoid, newValue)
end
end)
-- when the character model spawns into the world, update it immediately with the player's point size
player.CharacterAdded:Connect(function(character)
updateCharacter(character.Humanoid, pointsValue.Value)
end)
end)

The NPC follows the part but when i go near the NPC they leave the part and start going to random directions

So i'm trying to make an npc move to a part when i click on the npc, when i clicked on it all seemed well until i went near the npc, at that point it left the part and started moving to random directions. Is there something wrong?
Script:
local value = script.Parent.FollowSurvivor
local value2 = workspace.Values.MoveToValue
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 1000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait(0.5)
local target = findNearestTorso(script.Parent.Torso.Position)
if target ~= nil then
if value.Value == true then
script.Parent.Zombie:MoveTo(target.Position, target)
end
if value.Value == false then
script.Parent.Zombie:MoveTo(workspace.MoveToPart.Position)
end
end
end
This script gets the nearest humanoid and goes towards it. When you walked next to it, it found your humanoid and it started following you.

Roblox leaderstat only updates once

I'm trying to reward the player with exp every time they the punch a damageable object (punching bag, dummies, etc) Whenever the play punches something, it updates the exp leaderstat only once, does anyone know what i'm doing wrong?
My Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
script.Parent.Touched:Connect(function(hit)
local Char = hit.parent
local Hum = Char:FindFirstChild("Humanoid")
if Hum and Char.Name ~= script.Parent.Parent.Name then
local Indicator = require(game.ReplicatedStorage.DamageIndicator)
local Player = script.Parent.Parent
local LocalPlayer = game.Players:GetPlayerFromCharacter(Player)
local Exp = LocalPlayer.leaderstats.Experience.Value
Hum:TakeDamage(script.Dmg.Value)
Indicator.DamageActivate(script.Dmg.Value, hit)
Exp = Exp + 15
LocalPlayer.PlayerGui.UI.Experience.ExpBar.Size = UDim2.new((Exp / 100) * 0, 0, 0.02, 0)
LocalPlayer.PlayerGui.UI.Experience.ExpBackground.ExpAmt.Text = Exp.."/100"
script.Disabled = true
end
end)
You're having the same issue as this guy.
When you create a variable based on a NumberValue's Value, you are storing a copy of the value, not a reference to it. If you want to update the value, you need to manually assign it.
local Exp = LocalPlayer.leaderstats.Experience
Exp.Value = Exp.Value + 15
local ExpGui = LocalPlayer.PlayerGui.UI.Experience
ExpGui.ExpBar.Size = UDim2.new((Exp.Value / 100) * 0, 0, 0.02, 0)
ExpGui.ExpBackground.ExpAmt.Text = tostring(Exp.Value) .. "/100"

Roblox Studio Error: Expected ')' (to close '(' at column 18), got '='

I am trying to make a door move to a new position using the tween service. The problem is that I keep getting the error, Expected ')' (to close '(' at column 18), got '=', and I don't know why. It highlights the "=" next to "Position" on line 21 in red.
local service = game:GetService("TweenService")
local cupboard1 = game.Workspace.Door1
local cupboard2 = game.Workspace.Door2
local cupboard3 = game.Workspace.Door3
local cupboard4 = game.Workspace.Door4
local handle1 = game.Workspace.Handle1
local handle2 = game.Workspace.Handle2
local handle3 = game.Workspace.Handle3
local handle4 = game.Workspace.Handle4
local info = TweenInfo.new(2)
if state == true then
game.Workspace.Handle1.ClickDetector.MouseClick:connect(function()
local state = false
local change = (Position = Vector3.new(61.831, 4.997, 68.415)) -- door
local change1 = (Position = Vector3.new(63.401, 5.416, 67.89)) -- handle
local change2 = (Orientation = Vector3.new(0, 90, 0)) -- handle
local change3 = (Orientation = Vector3.new(0, 90, 0)) -- door
local tween = service:Create(cupboard1, info, change)
tween:Play()
local tween1 = service:Create(handle1, info, change1)
tween1:Play()
local tween2 = service:Create(handle1, info, change2)
tween2:Play()
local tween3 = service:Create(cupboard1, info, change3)
tween3:Play()
end)
end
if state == false then
game.Workspace.Handle1.ClickDetector.MouseClick:connect(function()
local state = true
local change = (Position = Vector3.new(58.112, 4.997, 66.588)) -- door
local change1 = (Position = Vector3.new(56.631, 5.416, 65.849)) -- handle
local change2 = (Orientation = Vector3.new(0, -135, 0)) -- handle
local change3 = (Orientation = Vector3.new(0, -135, 0)) -- door
local tween = service:Create(cupboard1, info, change)
tween:Play()
local tween1 = service:Create(handle1, info, change1)
tween1:Play()
local tween2 = service:Create(handle1, info, change2)
tween2:Play()
local tween3 = service:Create(cupboard1, info, change3)
tween3:Play()
end)
end
I've tried changing "=" to "==" but it gives me a warning saying Position and Orientation isn't defined.
You are trying to pass a table of changes into your tween. But when you use parenthesis, it is trying to evaluate the line, and this isn't valid lua.
Try changing your parentheses to curly brackets. This will properly assign the changes as a table, which you can pass to your tweens.
local change = { Position = Vector3.new(58.112, 4.997, 66.588)} -- door
local tween = service:Create(cupboard1, info, change)

How do I change a value in the leaderstats via touching a part? (Roblox)

Here's the error and what I've tried.
Error: Workspace.Part.Script:4: attempt to index nil with 'leaderstats'
My code:
Making Leaderstats:
game.Players.PlayerAdded:Connect(function(plr)
local ls = Instance.new("Model")
ls.Name = "leaderstats"
ls.Parent = plr
local m = Instance.new("IntValue")
m.Name = "Stars"
m.Value = 0
m.Parent = ls
end)
Touch Code:
local collected = false
script.Parent.Touched:Connect(function()
if collected == false then
game.Players.LocalPlayer.leaderstats.Stars.Value = game.Players.LocalPlayer.leaderstats.Stars.Value + 1
end
collected = true
end)
A server script would suit your need better. In a localscript, the change would only appear for the player. Also, it's better practice to use the Players service offered by Roblox. Here's an example:
local Players = game:GetService('Players')
local collected = false
script.Parent.Touched:Connect(function(partTouched)
if partTouched.Parent:IsA('Model') and partTouched.Parent:FindFirstChild('Humanoid') and partTouched:IsA('BasePart') then -- Is this a player? If so, then..
local player = Players:GetPlayerFromCharacter(partTouched.Parent)
if collected == false then
player.leaderstats.Stars.Value += 1 -- shorter syntax
end
collected = true
--script.Parent:Destroy() optional if this part won't be used again.
end
end)
If you're planning to use this for many parts, using a central ModuleScript would save you lots of changing things back and forth: https://developer.roblox.com/en-us/api-reference/class/ModuleScript