"Unable to cast value to Object" error message - lua

So I am using a remote function, as seen at the end, and for some reason, a normal assignment of a variable won't work, it is giving me the error message, "Unable to cast value to Object," what is wrong?
local storeEvent = script.Parent.Parent.OpenStore
local slotNum = 1
script.Parent.Touched:Connect(function (hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
storeEvent:InvokeClient(slotNum)
end
end)
and connecting to the other script:
script.Parent.OnInvoke:Connect(function (slot)
local StoreArrows = game.ReplicatedStorage.StoreArrows
StoreArrows.SlotNum.Value = slot
local cam = game.Workspace.Camera
local storeButtons = script.Parent
local camNum = game.ReplicatedStorage.StoreArrows.CamNum.Value
local camNumInst = game.Workspace.CamStorage:WaitForChild("Cam-"..camNum)
cam.CameraType = Enum.CameraType.Scriptable
cam.CFrame = camNumInst.CFrame
local clonedStoreButtons = StoreArrows:Clone()
clonedStoreButtons.Parent = player.PlayerGui.ScreenGui
end)

Keep in mind that many clients connect to a server at a time. So when you call a RemoteEvent's InvokeClient function, you have to tell it which client to invoke it on. The first parameter to InvokeClient is supposed to be the player, that's why the error is telling you that it cannot cast the slotNum value to a player object.
local storeEvent = script.Parent.Parent.OpenStore
local slotNum = 1
script.Parent.Touched:Connect(function(hit)
-- check that the thing that we touched is actually a player
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
-- tell that player to open the store
storeEvent:InvokeClient(player, slotNum)
end
end)

Related

DataStore not running

I'm trying to make a gamepass script where when you join you will be told to buy a gamepass. but When you join the game the Gamepass promt wont show up. and the output does not give me a error
local marketplaceservice = game:GetService("MarketplaceService")
local gamepassid = 11585012233
local function PromtPurchase()
local players = game.Players.LocalPlayer
local haspass = false
local success, message = pcall(function()
local hasspass =
marketplaceservice:PromptGamePassPurchase(players.UserId,gamepassid)
if haspass == true then
print("Player has pass")
marketplaceservice:PromptGamePassPurchase(players, gamepassid)
else
marketplaceservice:PromptGamePassPurchase(players, gamepassid)
end
end)
wait(1)
promtPurchase()
local hasspass = marketplaceservice:PromptGamePassPurchase(players.UserId,gamepassid)
This is a prompting function, so it'll just prompt every time, you want to be using
local hasspass = marketplaceservice:UserOwnsGamePassAsync(players.UserId, gamepassid)

Why is 'player' returning nil?

It keeps returning nil for player and saying that im trying to index a nil with 'WaitForChild' even though I have tried adding a wait command and changing player to 'game.Players.LocalPlayer' I'm new to scripting and I don't know what else to do.
local buyButton = script.Parent
local player = game:GetService("Players").LocalPlayer
local multiplier = player:WaitForChild("Multiplier")
buyButton.MouseButton1Up:Connect(function()
if multiplier.Value == 0 then
multiplier.Value = 1
end
end)
Instead of local player = game:GetService("Players").LocalPlayer you want to do:
local players = game:GetService("Players")
local player = players.localplayer

What does "attempt to index nil with 'WaitForChild'" mean?

script.Parent.MouseButton1Click:connect(function()
local RS = game:GetService("ReplicatedStorage")
local item = RS:WaitForChild("Pencil")
local price = 350
local player = game.Players.LocalPlayer
local stats = player:WaitForChild("leaderstats")
if stats.Strength.Value>=price then
stats.Strength.Value = stats.Strength.Value - price
local cloned = item:Clone()
cloned.Parent = player.Backpack
cloned.Parent = player.StarterGear
end
end)
I am trying to make a shop and it comes up with "attempt to index nil with 'WaitForChild'" on line 6:
local stats = player:WaitForChild("leaderstats")
I copied it exactly how the video had it and the video had no problem and apparently player has no value even though we set it up just one line above
It means that you are indexing a nil value, which means that player value is nil, which means that game.Players.LocalPlayer on the previous like returns nil. Why that is you need to figure out, as there is not much to go by.
The example shows that it should be local player = game:GetService("Players").LocalPlayer, so you may want to try that.

im making a roblox game and need to update the leaderboard stats when you click the baseplate

this is my code at the moment:
local players = game:WaitForChild("Players")
local function createLeaderboard(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
local baseclicks = Instance.new("IntValue", stats)
baseclicks.Name = "baseclicks"
stats.Parent = player
baseclicks.Value = 100
end
players.PlayerAdded:connect(createLeaderboard)
Im not sure if i need a clickdetector with a script inside or??
i dont know, please help.
If you don't want ClickDetectors on the Baseplate, simply, you could use mouse.Target. For example:
-- serverscript
local players = game:GetService("Players");
local function createLeaderboard(player);
local stats = Instance.new("Folder", player);
stats.Name = "leaderstats";
local baseclicks = Instance.new('IntValue', stats);
baseclicks.Name = 'baseclicks'
baseclicks.Value = 100;
end
-- localscript in startercharacterscript
local players = game:GetService("Players");
local client = players.LocalPlayer;
local mouse = client:GetMouse(); --method for getting the client's mouse
local event = game.ReplicatedStorage.OnClick --our remote event
local leaderstats = client.leaderstats or client:WaitForChild("leaderstats");
local clickvalue = leaderstats.baseclicks or leaderstats:WaitForChild("baseclicks");
mouse.Button1Down:Connect(function()
if not (mouse.Target) then return; end
if (mouse.Target.Name == "Baseplate") then
event:FireServer(clickvalue.Value + 1); --fire the remote event
end
end
end)
-- server script in serverscriptservice for remote event receiver
game.ReplicatedStorage.OnClick.OnServerEvent:Connect(function(Player, Value)
Player.leaderstats.baseclicks.Value = Value;
end
However, when it comes to the game having multiple players clicking at once, remote events are not recommended as they are more than likely going to lag the server or cause network traffic - you're better off using ClickDetectors.

Getting a nil response while trying to clone a object to backpack

I am trying to clone an object from the replicatedstorage to the players backpack when an object part is touched and the code looks fine for me but it keeps giving a nil response from clone.parent = player.backpack
local replicatedtorage = game:GetService("ReplicatedStorage")
local Sword = replicatedtorage:FindFirstChild("Sword")
local part = game.Workspace.Part
local player = game.Players.LocalPlayer
local clone = Sword:Clone()
part.Touched:Connect(function(hit)
local humanoid = hit.parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
clone.Parent = player.Backpack
end
end)
This looks like a server Script which cannot access Players.LocalPlayer like clients can because there is no local player to the server. A way to get the Player that touched a part is through Players:GetPlayerFromCharacter() which requires one instance to be passed and will either return the Player whose Character is that instance or nil.
part.Touched:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player then
clone.Parent = player.Backpack
end
end)
This should work right away in your script and can replace your existing Touched connection.

Resources