Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Part not cloning in players position?

Hello! I am trying to make a script when the player dies it clones a certain part in the players position, but it doesn't see to clone. Can anyone help me please?

Here is my current script:

local character = script.Parent
local humanoid = character:FindFirstChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(character)
local object = game.workspace.Food

humanoid.Died:Connect(function()
	print("Humanoid Died")
	print(player.Character.Head.Position)
	local ClonedObject = object:Clone()  
	ClonedObject.Parent = workspace
	ClonedObject.Name = ("Food")    
	ClonedObject.Position = player.Character.Head.Position
	print("Cloned (Hopefully)")	
end)

(If your wondering about the game, Im making a hunger system where the player has to touch the part to eat.) I've thought about instancing the part but its not possible because there is a value inside of it and if many players die the script wouldn't know what part to put the value in.

Sorry if this is a dumb question, kinda new to scripting.

6 comments

0

Anchor the part.

0

It is.

0

is it a model?

0

nevermind just noticed the comment on the other guys's answer.. also ("Food") looks ugly, try just "Food"

0

Im not sure if this will work but instead of having the ClonedObject.Parent be the workspace, make it the model in which the body parts of the player are in. So that would be local character probably.

0

Something else you did wrong was the fact that you did the local for the clone but never actually did "object:Clone()

2 answers

("Food") is not the proper way to do quotes, try "Food", and if that's not the problem try doing ClonedObject:MoveTo(player.Character.Head.Position) instead of ClonedObject.Position = player.Character.Head.Position

4 comments

0

It did clone, Just not in the right place.

0

do ClonedObject:MoveTo(player.Character.Head.Position) instead of ClonedObject.Position = player.Character.Head.Position

0

"MoveTo is not a valid member of Part"

0

Are you using MoveTo on a part or model?

I've figured it out myself, Thanks for the help tho. Heres what I did:

local char = script.Parent
local humanoid = char:FindFirstChild("Humanoid")
local Food = game.Lighting.Food
humanoid.Died:Connect(function()
	Food.CFrame = char.HumanoidRootPart.CFrame + Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))
	Food:Clone().Parent = game.Workspace
	Food:Clone().CanCollide = false
end)

I've revamped my code, The problem I found was that the player sometimes touched the part when they die, so it would get "eaten" even tho they're dead. Simple fix tho. Also I found that my hunger script was deleting it, so no wonder I couldn't see it. I tried moving the part to "Lighting" and it works fine.

Thanks everyone for the help.

1 comment

0

(I forgot to say how I fixed the "accidental eating" I basically just made it so that the part flings away from the player "Vector3.new(math.random(-5, 5), 0, math.random(-5, 5))")

This discussion is preserved as it was. New answers, comments, and votes are closed.