Roblox Item is lagging - lua

I add a mesh to follow the player where ever he goes. But the mesh is lagging a bit when the player runs. I understand that it is not rendering quickly enough, but anyone know how to add a mesh without it lagging?
local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
local backpackItemWorkspace = game.ReplicatedStorage.Meshes[pet.Name]:Clone()
backpackItemWorkspace.Parent = game.Workspace.CurrentPets
RunService.Stepped:Connect(function()
local location = humanoidRootPart.CFrame
backpackItemWorkspace.CFrame = location * CFrame.new(2, 2, 3)
end)

Connect the mesh and rootpart using a weld, so you dont need to use RunService to move the mesh everytime.
local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
local backpackItemWorkspace = game.ReplicatedStorage.Meshes[pet.Name]:Clone()
backpackItemWorkspace.Parent = game.Workspace.CurrentPets
function attach(hroot, mesh)
local weld = Instance.new("WeldConstraint", mesh)
local location = hroot.CFrame
mesh.CFrame = location + Vector3.new(2, 2, 3)
weld.Part0 = hroot
weld.Part1 = mesh
return weld
end
attach(humanoidRootPart, backpackItemWorkspace)
-- please comment if it makes any errors

Ok, for others struggling with having a pet(mesh) follow you smoothly and always stay on your back. I have now spent several hours on this subject and finally got it to work. This is how you do it:
local character = player.Character
local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
//where you copy you pet from
local backpackItemWorkspace = game.ReplicatedStorage.Meshes[pet.Name]:Clone()
//where you keep your pets in the workspace
backpackItemWorkspace.Parent = game.Workspace.CurrentPets
//call the function for attaching the pet
attachPet(backpackItemWorkspace, character, humanoidRootPart)
function attachPet (pet, char, humanoidRootPart)
local focusPart = humanoidRootPart
local newPet = pet
local z = -5
local x = 1
local bodyPos = Instance.new("BodyPosition")
bodyPos.Parent = newPet
bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
local bodyGyro = Instance.new("BodyGyro")
bodyGyro.Parent = newPet
bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
while wait() do
bodyPos.Position = focusPart.Position + focusPart.CFrame.LookVector * z + focusPart.CFrame.RightVector * x
bodyGyro.CFrame = focusPart.CFrame
end
end
Also when you unequip a pet you have to Destroy it from where you store it. In my case I store them in a folder on the player which I call collectionInventory.
local collectionInventory = player:WaitForChild("CollectionInventory")
collectionInventory[petName]:Destroy()
Hope this save anybody else from wasting several hours with research. Even though you will probably learn a lot from those "wasted hours" ;)

Related

Rotating a Axe Model in Roblox

So, For Context im trying to emulate kind of like throwing an axe, when you throw an axe it spins and spins and spins vertically.However, I got the spinning part right but the axis that is at, is not right the axe it should be facing vertically not facing you, i will post a gyazo link just so u can see how it spins right now. https://gyazo.com/99eaa8c1b5b3ce22205148509e115b14
Ive tried a couple things, ive tweend it, i thought that would work, as tweening it i can do it infinitley, But the result was it being still. I also tried pivoting it, But because of my courtine. Sometimes it wouldnt even go through
Heres The roblox code
local function ZodiacMove(plr,Mouse)
local TestingCourotine = coroutine.create(function(Handle)
while true do RunService.Stepped:Wait()
print(Handle)
Handle.CFrame = Handle.CFrame * CFrame.Angles(0,0,math.rad(10))
if Handle.Parent == nil then break end
end
print('the axe has vanished :sob:')
end)
local ZodiacProjectile = game:GetService("ReplicatedStorage").Axe:Clone()
local Handle = ZodiacProjectile:FindFirstChild("Blade")["Blade Tip"]
print(Handle)
local ZodiacProjectilePivot = ZodiacProjectile:GetPivot()
ZodiacProjectile:PivotTo(ZodiacProjectilePivot * CFrame.new(math.rad(10),0,0))
coroutine.resume(TestingCourotine,Handle)
---TweenInfoGoals.CFrame = Handle.CFrame * CFrame.Angles(0,0,math.rad(360))
--local ZodiacProjectileTween = TweenService:Create(Handle, tweenInfo,TweenInfoGoals)
--ZodiacProjectileTween:Play()
local OverParams = OverlapParams.new()
OverParams.FilterType = Enum.RaycastFilterType.Blacklist
OverParams.FilterDescendantsInstances = {workspace.Baseplate, plr.Character, plr.Character:GetChildren()}
local HumanoidRootPart = plr.Character:WaitForChild("HumanoidRootPart")
local BV = Instance.new("BodyVelocity")
BV.Parent = Handle
ZodiacProjectile.Parent = HumanoidRootPart
BV.Velocity = HumanoidRootPart.CFrame.LookVector * 100
BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
ZodiacProjectile:PivotTo( HumanoidRootPart.CFrame * CFrame.new(0, 0, -1))
Debris:AddItem(ZodiacProjectile,2)
end```

Roblox money distribution system not working

enter image description hereI was trying to make a money collection system for the person on the stone grey team, but it won't work. What it should do is take the wealth value from the province, multiply by 0.02 and add that onto the total amount of money for the person. The leaderboard has the 'Finances' on there, but the value stays at 0 no matter what. The output bar is also blank.
local map = game.Workspace.map
local rp = game:GetService("ReplicatedStorage")
local provinces = {
map.province1,
map.province2,
map.province3,
map.province4,
map.province5,
map.province6,
map.province7,
map.province8,
map.province9
}
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("BoolValue")
stats.Name = "leaderstats"
stats.Parent = plr
local finances = Instance.new("NumberValue")
finances.Name = "Finances"
finances.Parent = plr.leaderstats
end)
-- below here i have problems, above this seems to work normally
rp.TaxPaymentsCheque.OnServerEvent:Connect(function(plr)
for start = 1, #provinces do
local provincegrowing = provinces[start]
if provincegrowing.BrickColor == BrickColor.new("Dark stone grey") then
local finances = plr.leaderstats.Finances.Value
local wealth = provincegrowing.ProvinceWealth.Value
local taxmodifier = 0.02
local revenue = wealth * taxmodifier
local oldfinanceamount = finances
finances = oldfinanceamount + revenue
end
end
end)
There is also a picture if it can be of any use.
So the reason why you don't see any change is because you're not actually changing the Finances Value
Instead what you are doing is saving Finances' Value to a variable and changing the variable
local number = SomeIntValue.Value
number = number * 5
-- is practically the same as saying
local number = 1
number = number * 5
It doesn't actually set the IntValue's value
To do that, do something like this
local number = SomeIntValue.Value
number = number * 5
SomeIntValue.Value = number
-- or
SomeIntValue.Value = SomeIntValue.Value * 5

Not sure how to use vector3

I tried to make an item spawn every 60 seconds and in one of two locations, but when I tried to use vector3 it didn't spawn in the location of the "spawnplace1" or "spawnplace2", but it spawned ontop of the roka being copied and didn't move. Also I don't think I can hold the copied roka's either. Here's the code!
local roka = workspace["Rokakaka Fruit"]
local itemspawns = workspace.ItemSpawnLocals
local itemspawn1 = itemspawns["Item Spawn 1"]
local itemspawn2 = itemspawns["Item Spawn 2"]
local place1 = itemspawn1.Position
local place2 = itemspawn2.Position
wait(60)
local spawnplace1 = math.random(1,2)
local spawnplace2 = math.random(1,2)
if spawnplace1 == 1 then
roka2 = roka:Clone()
roka2.Parent = workspace
local roka2handle = roka2.Handle
roka2handle.Position = Vector3.new(itemspawn1)
elseif spawnplace1 == 2 then
roka2 = roka:Clone()
roka2.Parent = workspace
local roka2handle = roka2.Handle
roka2handle.Position = Vector3.new(itemspawn2)
end
print(spawnplace1)
print(spawnplace2)
The Vector3 holds coordinates of a point in 3D space. You have only provided 1 of 3 pieces of information in the constructor. To construct a Vector3, you need to provide the Y and Z axes as well, like this :
roka2handle.Position = Vector3.new(1, 2, 3)
But you don't need to explicitly create a Vector3 to get your code working. You can just assign the positions of the spawn locations to your newly created fruit, and that should do the trick. This way, you can add lots more spawn locations, and you don't need to update the script each time.
-- grab some things from the workspace
local roka = workspace["Rokakaka Fruit"]
local itemSpawns = workspace.ItemSpawnLocals
-- choose a random spawn location
local spawnLocations = itemSpawns:GetChildren()
local spawnNumber = math.random(1, #spawnLocations)
local spawnPosition = spawnLocations[spawnNumber].Position
-- spawn and move a new fruit to one of the spawn locations
roka2 = roka:Clone()
roka2.Parent = workspace
local roka2handle = roka2.Handle
roka2handle.Position = spawnPosition
-- debug
print("spawning fruit at : ", spawnPosition)
As a side note, if roka2 is a Model, you may want to consider using roka2:SetPrimaryPartCFrame( CFrame.new(spawnPosition)) to move it around.

Can't get to a value inside a player in lua

Code I Tried
game.Workspace.burgerclick.OnServerEvent:Connect(function()
local playername = game.Workspace.ClickPlayer.Value
local player = game.Players:FindFirstChild(playername)
local playaname = player.Name
local fpval = game.Players[playaname].FP.Value
fpval = fpval + 1
end)
I am making a game and I wanted to have a button that updates the value of something called FP, in the workspace I have a stringvalue called ClickPLayer which updates to the player clicking the button. that works. I made a variable that is the value the stringvalue, but when I put the variable inside the line when I update the FP Value it says the variable is not a valid member of players. I'm not sure what to do.
I think the correct code should be something like:
game.Players[playaname].FP.Value = game.Players[playaname].FP.Value + 1
Because, when you do:
local fpval = game.Players[playaname].FP.Value
fpval = fpval + 1
You are copying the value of game.Players[playaname].FP.Valueto fpval and incrementing the copy (fpval = fpval + 1), not the value held by game.Players[playaname].FP.
The most correct and efficient code would be
local Players = game:GetService("Players")
local fpval
local plr
workspace.burgerclick.Activated:Connect(function()
plr = workspace.ClickPlayer.Value
fpval = Players[plr].FP
fpval.Value = fpval.Value + 1
end)
In the documentation of TextButtons and ImageButtons, there is an Activated event that can be used for this, instead of firing a server from a LocalScript which is what your script aims to do now. However, if the button is in the PlayerGui and you have an activation LocalScript for it, you probably want to use this code instead:
local fpval
workspace.burgerclick.OnServerEvent:Connect(function(plr)
fpval = plr.FP
fpval.Value = fpval.Value + 1
end)

Saving Player Data in RLua

Is there a reason why these do not work?
Player joining script:
local DataStore = game:GetService("DataStoreService"):GetDataStore("GeneralStats")
game.Players.PlayerAdded:connect(function(player)
local stats = Instance.new("IntValue", player)
stats.Name = "leaderstats"
local points = Instance.new("IntValue", stats)
points.Name = "Points"
local credits = Instance.new("IntValue", stats)
credits.Name = "Credits"
local key = "player-"..player.userId
local savedValues = DataStore:GetAsync(key)
if savedValues then
--Save format: (points, credits)
points.Value = savedValues[1]
credits.Value = savedValues[2]
else
local ValuesToSave = {points.Value, credits.Value}
DataStore:SetAsync(key, ValuesToSave)
end
end)
And this other script for when the player leaves.
local DataStore = game:GetService("DataStoreService"):GetDataStore("GeneralStats")
game.Players.PlayerRemoving:connect(function(player)
local key = "player-"..player.userId
--Save key: {points, credits}
local valuesToSave = {player.leaderstats.Points.Values, player.leaderstats.Credits.Values}
DataStore:SetAsync(key, valuesToSave)
end)
This is for a game I am working on, justify (RLua is Roblox Lua, if you didn't know).
Yes, more than likely the leaderstat is being removed before you have a chance to extract data.
I would suggest not using leaderstats as reference for data. It's better to store data on the script directly.
However, if you truly must use leaderstats, parent it to somewhere else, extract data, then remove it.
local lead = player.leaderstats
lead.Parent = game
-- extract data
lead:Destroy()
Or you could define all those objects in a variable before they get reparented.
But again, I highly suggest not to use leaderstats for data saving. Exploiters can change that data easily and change the value to a high number.

Resources