robloxapp-20240201-1739300.wmv (3.7 MB)
Is it possible you tagged the welded part with apple? Also here is an improved version of the script which doesnât create a bunch of loops:
local collectionService = game:GetService("CollectionService")
local runService = game:GetService("RunService")
local height = 2
local speed = 1
local storedYValues = {}
local function trig(x,height,speed,f)
return height*f(speed*x)
end
runService.RenderStepped:Connect(function(dt)
local apples = collectionService:GetTagged("Apple")
local sin = trig(tick(),height,speed,math.sin)
local cos = trig(tick(),height,speed,math.cos)
for i,v in apples do
local Position,Orientation = v.Position,v.Orientation
local ogY = storedYValues[v] or Position.Y; storedYValues[v] = ogY
v.Position = Vector3.new(Position.X,ogY+sin,Position.Z)
v.Orientation = Vector3.new(Orientation.X,Orientation.Y+math.abs(cos),Orientation.Z)
end
end)
(sorry for the late reply)
Nope. wshfbwefedwqefgefdweqef
local function createFood()
local food = game:GetService("ServerStorage").Apple:Clone()
food.PrimaryPart.CFrame = CFrame.new(math.random(-100, 100), 2, math.random(-100, 100))
food.Parent = folder
CS:AddTag(food, "Apple")
end
Btw this uses a lot of math wth.
Damn, how I didnât know this before. Also why donât you use Tweenâs, so I mean you can set the Tween to reverse itself using TweenInfo
Iâm gonna move the apple to random different positions while keep moving up and down. If I use tweens, it wonât work.
Its uses sin and cos so the spinning eases to a stop at the top instead of the middle. (Cos is a translated version of Sin along the x-axis). How did the code go?
Then try using this, it might work.
Vector3.new(Part.Position.X, 5, Part.Position.Z)
Iâm sorry but Im still 9th grade so I didnât learn sin, cos and tan etc.
Wait, I donât even understand the code fully. And does it run on the client?
Sin simplifies this, it returns both positive and negative values. So, the code here will need a positive and a negative value.
It runs on the client, Ill explain each part for you.
local function trig(x,height,speed,f)
return height*f(speed*x)
end
this is the basic graph equation for trig functions (Sin,Cos,Tan)
y = a sin(bx)
B is the period, how long it takes for the wave to do a full circle (the image is a circle) The period is measured by 2Ď/b
A is the Amplitude, which is the distance of the turning points from X=0 (in the image amplitude is 1)
This is why in the script A is labelled as Height
The difference between a sin and a cos graph is a cos graph is translated by Ď/2
:
local sin = trig(tick(),height,speed,math.sin)
local cos = trig(tick(),height,speed,math.cos)
so here I get a sin and cos value using the function, utilizing x as tick()
which is just a seconds count from the 1st of January of 1970. Since its a number value that keeps going up its useful compared to making a counter variable.
local Position,Orientation = v.Position,v.Orientation
local ogY = storedYValues[v] or Position.Y; storedYValues[v] = ogY
v.Position = Vector3.new(Position.X,ogY+sin,Position.Z)
v.Orientation = Vector3.new(Orientation.X,Orientation.Y+math.abs(cos),Orientation.Z)
this part just stores/fetches the starting Y value using a dictionary and positions the part based on sin (a number from -height to height).
With the 4th line, math.abs The absolute
is used to make sure that the value is never negative so it doesnât spin back and fourth and instead keeps spinning forward.
If any of this doesnât make sense lmk.
Btw the apple is moving up and down smoothly, however the same problem occurs.
The welded part is spinning crazily?
For this function it looks like your tagging a model with apple? If this was the case it would cause an error and the apples would not be moving at all. Are you sure that they arenât already tagged in the workspace?
This is the current code.
local collectionService = game:GetService("CollectionService")
local runService = game:GetService("RunService")
local height = 2
local speed = 1
local storedYValues = {}
local function trig(x,height,speed,f)
return height*f(speed*x)
end
runService.RenderStepped:Connect(function(dt)
local apples = collectionService:GetTagged("Apple")
local sin = trig(tick(),height,speed,math.sin)
local cos = trig(tick(),height,speed,math.cos)
for i,v in apples do
local Position,Orientation = v.Position,v.Orientation
local ogY = storedYValues[v] or Position.Y; storedYValues[v] = ogY
v.PrimaryPart.Position = Vector3.new(Position.X,ogY+sin,Position.Z)
v.PrimaryPart.Orientation = Vector3.new(Orientation.X,Orientation.Y+math.abs(cos),Orientation.Z)
end
end)
This is the code that adds the tag.
local function createFood()
local food = game:GetService("ServerStorage").Apple:Clone()
food.PrimaryPart.CFrame = CFrame.new(math.random(-100, 100), 2, math.random(-100, 100))
food.Parent = folder
CS:AddTag(food, "Apple")
end
In-game, the only one that is tagged is the model, not the children.
This is the code that makes the part move when hit.
Players.PlayerAdded:Connect(function(player) -- fires when player joins
player.CharacterAdded:Connect(function(character)
local personalData = {} -- store personal data of each character
personalData["CharacterPosition"] = character.PrimaryPart.Position -- the character's original position
personalData["PartsList"] = {}
characterDatas[character] = personalData -- put personalData in the characterDatas table
character.PrimaryPart.Touched:Connect(function(hitPart)
if hitPart.Parent.Parent ~= folder then return end -- we want it to only touch apple
print("yes")
hitPart.Parent.PrimaryPart.CFrame = CFrame.new(math.random(-100, 100), 2, math.random(-100, 100))
if the model is tagged Apple
then the code would error. Could you show me the output?
Oh yeah, code error.
But, if I do this,
local collectionService = game:GetService("CollectionService")
local runService = game:GetService("RunService")
local height = 2
local speed = 1
local storedYValues = {}
local function trig(x,height,speed,f)
return height*f(speed*x)
end
runService.RenderStepped:Connect(function(dt)
local apples = collectionService:GetTagged("Apple")
local sin = trig(tick(),height,speed,math.sin)
local cos = trig(tick(),height,speed,math.cos)
for i,v in apples do
local Position,Orientation = v.PrimaryPart.Position,v.PrimaryPart.Orientation
local ogY = storedYValues[v] or Position.Y; storedYValues[v] = ogY
v.PrimaryPart.Position = Vector3.new(Position.X,ogY+sin,Position.Z)
v.PrimaryPart.Orientation = Vector3.new(Orientation.X,Orientation.Y+math.abs(cos),Orientation.Z)
end
end)
Ok, well things messed up.
Full circle? you mean wave? and what is 2Ď/b
? what is b
?
Ye a wave. B is from the equation above: y = a*sin(b*x)
Ok, Iâm so stupid. So, the apple moved. It was because of another script that is also making the apple move. However, there are still problems.
robloxapp-20240204-1319588.wmv (2.2 MB)
The script activity is also so high.