i’m trying to do move a model when a player touched a Part but the script I made don’t work.
(here the script)
script.Parent.PrimaryPart = script.Parent.center
local debounce = true
script.Parent.BottomPart.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid and debounce == true then
debounce = false
for i = 1,5 do
script.Parent:SetPrimaryPartCFrame(CFrame.new(script.Parent.PrimaryPart.Position + Vector3.new(0,1,0)))
debounce = true
end
end
end)
Thx for your help
If you have sugestion to improve the script
Hello, first off; why are you using a numeric loop to move a model?
Just use TweenService if you want to animate the movement of the model.
I would recommend you to use the CFrame property instead of the :SetPrimaryPartCFrame function. What you could do then is;
local time = 0.5 -- duration of your Tween in seconds
local newCoordinateFrame = (script.Parent.CFrame + Vector3.new(0,5,0))
game:GetService("TweenService"):Create(script.Parent, TweenInfo.new(time, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {CFrame = newCoordinateFrame}):Play()
for that to work just get rid of the numeric loop.
If you have any further questions just reply to me, also if the code does not work send me the Console Output. (Code untested)
local time = 0.5 -- duration of your Tween in seconds
local newCoordinateFrame = (script.Parent.CFrame + Vector3.new(0,5,0))
local Tpart = script.Parent.BottomPart
local debounce = true
Tpart.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
if humanoid and debounce == true then
debounce = false
game:GetService("TweenService"):Create(script.Parent, TweenInfo.new(time, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {CFrame = newCoordinateFrame}):Play()
debounce = true
end
end)
I maybe did an error Here’s the OutPut
17:47:48.560 CFrame is not a valid member of Model "Workspace.all.level1.Brick.Brick Block1" - Serveur - Script:2
17:47:48.560 Stack Begin - Studio
17:47:48.561 Script 'Workspace.all.level1.Brick.Brick Block1.Script', Line 2 - Studio - Script:2
17:47:48.561 Stack End - Studio
Read the Output. Models don’t have a Position or CFrame Property, it needs to be a Part, a UnionOperation aka Union, a MeshPart or any part that has a Position / CFrame property.