I am trying to create a part which turns a player into a robot and give them a laser gun when they touch the part. The morphing part works fine, but the laser gun-giving part doesn’t, and I do not know why because there are no errors. Please help if you can!
CODE:
local lasers = game:GetService("ReplicatedStorage"):WaitForChild("Lasers")
local morph = game.ServerStorage.Morphs.EvilRobot
local debounce = true
script.Parent.Touched:Connect(function(obj)
local plr = game.Players:GetPlayerFromCharacter(obj.Parent)
local backpack = plr:FindFirstChildOfClass("Backpack")
if plr and debounce == true then
debounce = false
local morphClone = morph:Clone()
morphClone.Name = plr.Name
plr.Character = morphClone
local rootPart = morphClone:FindFirstChild("HumanoidRootPart") or morphClone:FindFirstChild("Torso")
local plrRoot = obj.Parent:FindFirstChild("HumanoidRootPart") or obj.Parent:FindFirstChild("Torso")
if rootPart and plrRoot then
rootPart.CFrame = plrRoot.CFrame
end
morphClone.Parent = workspace
if backpack then
local laserClone = lasers:Clone()
laserClone.Parent = backpack
end
wait(.5)
debounce = true
end
end)
--this is the code which gives the player the tool
if backpack then
local laserClone = lasers:Clone()
laserClone.Parent = backpack
end
Oh ok, so if there’s no error then I’m guessing the player’s character isn’t changing correctly. I’d probably just try cloning the morph into the StarterCharacter folder, and naming it “StarterCharacter” then calling player:LoadCharacter() and then destroying the morph afterwards.
Ok, I’m not sure what the problem may be, but I recreated script with what I believe your assets look like, and it works nicely:
local part = script.Parent
local morph = game:GetService("ServerStorage"):WaitForChild("Morphs").EvilRobot
local lasers = game:GetService("ReplicatedStorage"):WaitForChild("Lasers")
local debounce = false
part.Touched:Connect(function(obj)
if obj ~= nil and obj.Parent:FindFirstChild("Humanoid") ~= nil and game:GetService("Players"):FindFirstChild(obj.Parent.Name) ~= nil and debounce == false then debounce = true
local plr = game:GetService("Players"):GetPlayerFromCharacter(obj.Parent)
local morphClone = morph:Clone()
morphClone.Name = "StarterCharacter"
morphClone.Parent = game:GetService("StarterPlayer")
local lastCharacterPos = plr.Character:WaitForChild("HumanoidRootPart").Position
plr:LoadCharacter()
morphClone:Destroy()
local lasersClone = lasers:Clone()
lasersClone.Parent = plr:WaitForChild("Backpack")
plr.Character:MoveTo(lastCharacterPos + Vector3.new(0, 0, -5))
debounce = false
end
end)