Can't get to a value inside a player in lua - 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)

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 Item is lagging

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" ;)

argument 1 missing or nil

i am making a tower defense game but it keeps saying argument 1 missing or nil
when i try to spawn the tower
this is a module script
(error at line 11)
local PhysicsServive = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PhysicsService = game:GetService("PhysicsService")
local events = ReplicatedStorage:WaitForChild("Events")
local Tower = {}
local SpawnTowerEvent = events:WaitForChild("SpawnTower")
function Tower.Spawn(player, Name, CFrame)
local towerExists = ReplicatedStorage.Towers:FindFirstChild(Name)
if towerExists then
local newTower = towerExists:Clone()
newTower.HumanoidRootPart.CFrame = CFrame
newTower.Parent = workspace.Towers
newTower.HumanoidRootPart:SetNetworkOwner(nil)
for i, object in ipairs(newTower:GetDescendants()) do
if object:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(object, "Tower")
object.Material = Enum.Material.ForceField
end
end
else
warn("Missing:", Name)
end
end
SpawnTowerEvent.OnServerEvent:Connect(Tower.Spawn())
return Tower
SpawnTowerEvent.OnServerEvent:Connect(Tower.Spawn())
Connect expects a function value, not a function call (unless that function call resolves to a function value). Remove the call operator ().
SpawnTowerEvent.OnServerEvent:Connect(Tower.Spawn)
You call Tower.Spawn without any arguments. Therefor you call FindFirstChild(nil) which causes the observed error.
Also it does not return a function value.

Roblox Lua trying to tween part to move back and fourth like a platform

I've been practicing tweening on Lua and I'm struggling to understand why I can't get my platform to move back and fourth between a start and an end platform. First I'll show the code
-- Starting Variables
local TweenService = game:GetService("TweenService")
local group = game.Workspace.MovingPlatform
-- Group Variables
local part = group.Platform
local start = group.Check1
local finish = group.Check2
-- Vectors
local destination = Vector3.new(start.Position.x,start.Position.y, start.Position.z)
-- Platform Tween Info
local info = TweenInfo.new(
1, --Length (seconds)
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
-1,--Times To Be Repeated
true,
0 --Delay
)
-- Where the destination is
local Goals = {
Position = Vector3.new(destination)
}
-- Makes it go
local MovePart = TweenService:Create(part, info, Goals)
MovePart:Play()
-- Debugging
local startPosition = Vector3.new(start.Position.x, start.Position.y, start.Position.z)
local endPosition = Vector3.new(finish.Position.x, finish.Position.y, finish.Position.z)
local partPosition = Vector3.new(part.Position.x, part.Position.y, part.Position.z)
while true do
print("Start Position: "..tostring(startPosition))
print("Destination: "..tostring(destination))
print("End Position: "..tostring(endPosition))
print("Platform Position: "..tostring(partPosition))
print("--------------")
wait(3)
end
The platform will move once and then start bugging out and going anywhere and eventually will rest on the floor moving back and fourth but not at the correct location. I've tried debugging to see if the positioning of any of my parts were somehow changing but everything stayed the same. Maybe I'm not logging the positions correctly but nonetheless any hints at what I could be doing wrong?
You might want to try using CFrame instead of Position when moving things.
Also, Part.Position is a Vector3 so there's no point in creating a new Vector3 for a Vector3.
Try something like this:
-- Starting Variables
local TweenService = game:GetService("TweenService")
local group = game.Workspace.MovingPlatform
-- Group Variables
local part = group.Platform
local start = group.Check1
local finish = group.Check2
-- Set the platform part to start at Check1
part.CFrame = start.CFrame
-- Platform Tween Info
local info = TweenInfo.new(
1, --Length (seconds)
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
-1,--Times To Be Repeated
true,
0 --Delay
)
-- Where the destination is
local Goals = {
CFrame = finish.CFrame -- We want it to end at the finish part's position
}
-- Makes it go
local MovePart = TweenService:Create(part, info, Goals)
MovePart:Play()

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