BodyPosition not changing? - roblox

I've been sitting at my laptop for about an hour trying to figure out what's going on. I did this exact same thing yesterday and it worked. Please help.
Explaining the program:
The program takes the object that the mouse is pointing at, if you click the object gravitates toward the mouse.
local player = game.Players.LocalPlayer
local runservice = game:GetService("RunService")
local character = player.Character
local humanoid = character.Humanoid
local camera = workspace.Camera
local mouse = player:GetMouse()
local mousepos = mouse.hit.p
local grabbed = false
local function grabbedobj(obj) --this is the thing i need help on
local bodypos = obj.BodyPosition.Position
local pos = obj.Position
obj.Anchored = false
mouse.Button1Down:Connect(function()
grabbed = true
end)
mouse.Button1Up:Connect(function()
grabbed = false
end)
if grabbed == true then
print(obj.BodyPosition.Position)
bodypos = Vector3.new(mousepos.X, mousepos.Y, mousepos.Z) --really important
end
if grabbed == false then
bodypos = Vector3.new(pos.X, -25, pos.Z)
pos = Vector3.new(bodypos.X, pos.Y, bodypos.Z)
end
end
local function setup()
mousepos = mouse.hit.p
if mouse.Target ~= nil and mouse.Target.Parent == game.Workspace.IntObj then
grabbedobj(mouse.Target) --takes object that the mouse is pointing at
end
end
while wait(.1) do
setup()
end

Lets shuffle the code a little bit so it does what I think you want to do. You might have mixed up some things while trying to debug this. So here:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local grabbed = false
local obj
mouse.Button1Down:Connect(function()
if mouse.Target ~= nil and mouse.Target.Parent == game.Workspace.IntObj then
obj = mouse.Target
obj.Anchored = false
grabbed = true
end
end)
mouse.Button1Up:Connect(function()
if (grabbed) then
grabbed = false--BodyPosition
obj.BodyPosition.Position = Vector3.new(obj.Position.X, 0.1, obj.Position.Z)
obj.Anchored = true
end
end)
local function move()
if (grabbed) then
local mousepos = mouse.hit.p
obj.BodyPosition.Position = Vector3.new(mousepos.X, mousepos.Y, mousepos.Z)
print(obj.BodyPosition.Position)
end
end
while wait(.1) do
move()
end
When you click the mouse it checks if you click your part that is in IntObj. It will retain a reference to that part and then moves it with the mouse. When you release the button it will drop it at its location.
So this LocalScript should work but it has an issue updating the BodyPosition as you said in the title. The reason for that is that you can only update server parts from your client if you give the client permission from the server for it. You could write a remote function on the server and say:
game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function(plr)
Workspace.IntObj.Part:SetNetworkOwner(plr)
end
If you call this from your local script's initialization, it will work. Note, that your part cannot be anchored for that.
Another way would be to just let the server do the updating. So instead of setting the
obj.BodyPosition.Position on the client you would call the remote function like this:
game.ReplicatedStorage.RemoteFunction:InvokeServer(Vector3.new(mousepos.X, mousepos.Y, mousepos.Z))
and then on the server you set the position:
game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function(plr, bodyPos)
script.Parent.BodyPosition.Position = bodyPos
end
In that case, if you anchor/unanchor the part, do that on the server as well. Otherwise you'll get weird results.

Related

Roblox Studio-DataStore request was added to queue

I am making Race clicker game
And this is my script of my code
and I am keep getting the error, "DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 2222391052"
And the leaderstats are not updating.
local dataStoreService = game:GetService("DataStoreService")
local leaderstatsDataStore = dataStoreService:GetDataStore("data")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local passions = Instance.new("IntValue", leaderstats)
passions.Name = "Passion"
local upgrades = Instance.new("IntValue", leaderstats)
upgrades.Name = "Hero Upgrade"
local MaxSpeed = Instance.new("IntValue",leaderstats)
MaxSpeed.Name = "Max Speed"
local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId)
if leaderstatsDataStore ~= nil then
passions.Value = leaderstatsData[1]
upgrades.Value = leaderstatsData[2]
MaxSpeed.Value = leaderstatsData[3]
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local values = {}
for _, child in pairs(player.leaderstats:GetChildren()) do
table.insert(values,child.Value)
end
pcall(function()
leaderstatsDataStore:SetAsync(player.UserId, values)
end)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local values = {}
for _, child in pairs(player.leaderstats:GetChildren()) do
table.insert(values,child.Value)
end
pcall(function()
leaderstatsDataStore:SetAsync(player.UserId, values)
end)
end
end)
Someone said that this is an error that happens if I send request more than 60 times a second.
But I can't really get what is sending so much requests.
So can anyone tell the changed code to me
That's not an error, that's a warning. (Errors are red, warnings are yellow.)
You are getting the warning because the server can't do multiple data store operations at once. Instead, it queues the operations and does them one by one. However, if the queue becomes too long, some requests get dropped. That's not happening in your case, so you don't have to care about it.

I can't get my Data Store V2 to work, It always returns nil

I read the Data Store V2 documentation on the Dev Hub and tried to make a similar code, but I can't get it to work. It doesn't have any errors other than Attempt to index nil with Version because it can't get the Data. It returns Succes = true, and CurrentNumber, KeyInfo = nil. There are no errors when saving the data and the Succes variable is always returned true. I enabled the security configuration to allow data stores, the game is public and I tested it both on studio and the actual game(on Roblox). The code is in a script in the Server Script Service Here's the code:
local DataStoreService = game:GetService('DataStoreService')
local Players = game:GetService('Players')
-- Data Stores:
-- Enables Data Stores V2
local Options = Instance.new('DataStoreOptions')
Options:SetExperimentalFeatures({["v2"] = true})
local NumberStore:DataStore = DataStoreService:GetDataStore("PlayerNumber", "global", Options)
local SetOptions = Instance.new('DataStoreSetOptions')
SetOptions:SetMetadata({['PlayerNumberType'] = 'Int'})
local function SaveNumberData(Player:Player)
local Number = 1
local PlayerKey = 'Player_1234'
local Sucess, Errormessage = pcall(function()
NumberStore:SetAsync(PlayerKey, Number, {Player.UserId}, SetOptions)
end)
if not Sucess then
print(Errormessage)
else
print('Data Saved')
end
end
local function LoadPlayerData(Player:Player)
local PlayerKey = 'Player_1234'
local Sucess, CurrentNumber, KeyInfo:DataStoreKeyInfo = pcall(function()
NumberStore:GetAsync(PlayerKey)
end)
if Sucess then
print(CurrentNumber)
print(KeyInfo.Version)
print(KeyInfo.CreatedTime)
print(KeyInfo.UpdatedTime)
print(KeyInfo:GetUserIds())
print(KeyInfo:GetMetadata())
else
print(CurrentNumber)
end
end
Players.PlayerRemoving:Connect(SaveNumberData)
Players.PlayerAdded:Connect(LoadPlayerData)
I found the answer, I forgot to put return before NumberStore:GetAsync(PlayerKey)

Put an object in the inventory with Roblox Studio

When I put an object in the backpack, the Explorer window shows these objects, but the inventory displayed stays empty.
My code :
local objets = game.ServerStorage.aRamasser
local nbMaxObjets = 10
wait(5)
objets.Parent = game.Workspace
for indice,unObjet in pairs(objets:GetChildren()) do
local ClickDetector = unObjet:FindFirstChild("ClickDetector")
ClickDetector.MouseClick :Connect(
function (joueur)
local inventaire = joueur:FindFirstChildOfClass("Backpack")
if inventaire then
local nbObjets = table.getn(inventaire:GetChildren())
if nbObjets < nbMaxObjets then
(unObjet:Clone()).Parent = inventaire
unObjet.Transparency = 1
unObjet.CanCollide = false
wait(15)
unObjet.Transparency = 0
unObjet.CanCollide = true
end
end
end
)
end
Explanation :
The objects are in the directory ServerStorage. At the begining of the game, in the script above (in Workspace), this directory is moved into Workspace. The objects are visible.
Then, if a click is detected on one object, this object is cloned and that clone is put in Backpack. It works, I see them in Explorer. But not in the inventory :
Your object needs to be inside a tool, tool can come in the inventory gui.
local tool = Instance.new("Tool")
tool.Parent = inventaire
(unObjet:Clone()).Parent = tool

Roblox: BindToClose works, PlayerRemoving does not

I am trying to store CFrame X, Y, and Z data into DataStore so that the player can start off where they left off. I am testing by trying to save hardcoded values.
The loading of data using PlayerAdded works fine.
For some reason, the SetAsync() in PlayerRemoving wasn't completing when the player leaves in game or in studio. The weird thing is that when I added BindToClose, the save function runs correctly when in Studio but not in game.
If anyone can tell me why, I'd be very grateful.
local function save(plr)
local data = {
CFrameX = -1232;
CFrameY = 1232;
CFrameZ = -1235;
}
local success, errormessage = pcall(function()
MandoSimStore:SetAsync(plr.UserId.."-Location_StorageData", data)
end)
if success then
print("Successfully saved data!")
else
warn("ERROR: "..errormessage)
end
end
local gameShutDown = false
game.Players.PlayerRemoving:Connect(function(plr)
wait(0.1)
if not gameShutDown then
save(plr)
end
end)
game:BindToClose(function()
gameShutDown = true
for _, plr in ipairs(game.Players:GetPlayers()) do
save(plr)
end
end)
Maybe try
wait(1)
It worked for me

How to make a Roblox script search a player's backpack

I am trying to make a script that searches a players backpack when they touch a door, so it can tell if the player has a key card. If the player has a key card, it should say "Yes", but for some reason it keeps bringing up an error. Here is my code:
function onTouched(m)
p = m.Parent:findFirstChild("Humanoid")
if p ~= nil then
n = p.parent
local letin = game.Players(n).Backpack:FindFirstChild("Key Card")
if letin then
print("Yes")
else
print("No")
end
end
end
script.Parent.Touched:connect(onTouched)
The error is:
Trying to call method on object of type: 'Player' with incorrect arguments.
Does anyone know why this might not work?
I think you've got two problems :
It looks like you are trying to access an array index, but you're using () instead of [].
The game.Players object is a service class, not an array. But you can call game.Players:GetPlayers() to get that array of the players.
Since you are already getting the player object, you can simply grab the player's name and use that to lookup the player from game.Players.
Your script just about works, here's a fix for you :
function onTouched(m)
-- get the player in game that touched the thing
local p = m.Parent:findFirstChild("Humanoid")
if p ~= nil then
-- grab the player's name
local n = p.parent.Name
-- find the player in the player list, escape if something goes wrong
local player = game.Players:FindFirstChild(n, false)
if player == nil then
return
end
-- search the player's backpack for the keycard
local keycard = player.Backpack:FindFirstChild("Key Card")
if keycard then
print("Yes")
else
print("No")
end
end
end
script.Parent.Touched:connect(onTouched)
The following line of code is incorrect, because instead of finding a child in
'game.Players', you call it as a function by using parenthesis.
local letin = game.Players(n).Backpack:FindFirstChild("Key Card")
What you probably intended was:
local letin = game.Players[n].Backpack:FindFirstChild("Key Card")
A more clean approach to getting that 'Key Card' is to also check if 'n' really is the name of a player, rather than that of a NPC.
local player = game.Players:FindFirstChild(n)
if player then
local letin = player.Backpack:FindFirstChild("Key Card")
print(letin)
end