Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[FE]Trail Shop Not Working? Error: "Unable to cast value to Object"

Asked by
PolyyDev 214 Moderation Voter
6 years ago

I have a ShopGui and the main script in there is working and Firing a RemoteEvent and the script for the Event is in ServerScriptService. The shop item is in the gui in a folder. Here is the main script:

--// Variables
local sgui = script.Parent.Parent
local misc = sgui.Misc
local guis = sgui.Guis
local scripts = sgui.Scripts
local values = sgui.Values

local I1 = guis.Trails.Item1:WaitForChild("BuyT")
local I2 = guis.Trails.Item2:WaitForChild("BuyT")
local I3 = guis.Trails.Item3:WaitForChild("BuyT")
local I4 = guis.Trails.Item4:WaitForChild("BuyT")
local I5 = guis.Trails.Item5:WaitForChild("BuyT")
local I6 = guis.Trails.Item6:WaitForChild("BuyT")
local I7 = guis.Trails.Item7:WaitForChild("BuyT")
local I8 = guis.Trails.Item8:WaitForChild("BuyT")
local I9 = guis.Trails.Item9:WaitForChild("BuyT")
local IX = guis.Trails.ItemX:WaitForChild("BuyT")

local bt = game:GetService("ReplicatedStorage").BuyTrail
local mb = game:GetService("ReplicatedStorage").MoveBlur

--// Code

I1.MouseButton1Click:Connect(function()
    bt:FireClient("Trail1")
end)

I2.MouseButton1Click:Connect(function()
    bt:FireClient("Trail2")
end)

I3.MouseButton1Click:Connect(function()
    bt:FireClient("Trail3")
end)

I4.MouseButton1Click:Connect(function()
    bt:FireClient("Trail4")
end)

I5.MouseButton1Click:Connect(function()
    bt:FireClient("Trail5")
end)

I6.MouseButton1Click:Connect(function()
    bt:FireClient("Trail6")
end)

I7.MouseButton1Click:Connect(function()
    bt:FireClient("Trail7")
end)

I8.MouseButton1Click:Connect(function()
    bt:FireClient("Trail8")
end)

I9.MouseButton1Click:Connect(function()
    bt:FireClient("Trail9")
end)

IX.MouseButton1Click:Connect(function()
    bt:FireClient("TrailX")
end)

And here is the event script:

local bt = game:GetService("ReplicatedStorage").BuyTrail
local mb = game:GetService("ReplicatedStorage").MoveBlur



bt.OnClientEvent:Connect(function(player, trail)
    print("Attempting To Buy Trail")
    local t = player.PlayerGui.TrailShop:WaitForChild("Trails"):FindFirstChild(trail):Clone()
    player.CharacterAdded:Connect(function(char)
        t.Parent = char.HumanoidRootPart
        local a0 = Instance.new("Attachment",char.Head)
        a0.Name = "Attach0"
        local a1 = Instance.new("Attachment",char.HumanoidRootPart)
        a1.Name = "Attach1"
        t.Attachment0 = a0
        t.Attachment1 = a1
    end)


end)

mb.OnServerEvent:Connect(function()

end)

I get an error when I click the button that says "Unable to cast value to Object" for whatever line the button I clicked on for example if I clicked the first one it would be on line 24.

I'm really not sure what is happening as I am new to FE scripting. Some help would be appreciated. And if you could test it before you post it to make sure it works.

Take this if you need a better reference: https://www.roblox.com/library/1111083460/Shop-Need-Help

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

The error is that the first argument of each FireClient function expects a player, and so Roblox is trying (and failing) to convert the string you've provided into a player. Instead, use FireAllClients.

Do note that server scripts do not usually have access to a player's GUI. The server should handle purchases, awarding players items, etc, and the client should handle the GUI completely. The client must notify the server when a player wishes to buy something, for instance.

The same script should not have both OnClientEvent and OnServerEvent -- either's a server script or a client script, never both (except for the special case when you're testing it in Studio without using the "Start Server" option).

[Edit]

You requested help fixing your scripts, so I did (and uploaded a fixed version here: https://www.roblox.com/library/1114647229/Ki-ngPhantoms-Shop-Fixed

(Note that I only did this because I felt like it -- this site is not a request site. You're supposed to try and make the changes yourself and then ask for help when you don't know how to proceed.)

If you want to get good at scripting, study the scripts and try to understand them (and feel free to ask about them on this site, or even PM me. You can also email me at [email protected]. Some topics you'll want to learn about if you want to get better:

  • Learn about for loops and tables. Notice how you have the same sort of code over and over again, one for each item? For loops and tables are an important step in solving this.
  • Making functions to handle duplicate code is also important. Specifically, whenever you notice you've got the same bit of code in 2+ places, put it in a function instead. Combined with for loops and tables, you can start doing powerful things.
  • I invented a "class" called PlayerData. Lua doesn't officially support classes, but there are a number of ways to simulate them. In Roblox there are many classes -- ex, Part, Frame, Trail, etc. Some important definitions to understand my comments:
    • private: Means that the function or variable is not meant to be used outside the class.
    • public: Means that the function or variable is meant to be used outside the class.
    • It is common to make a variable private but then give public functions for outside code to access it. As I explain in the script, this is beneficial when you want to control how the variable is used -- you can make sure to define the "proper way" to use a variable, rather than allowing outside code to change it however it likes.
  • If you want to know more about classes, you might look up "Object Oriented Programming" or "classes in lua".

Other notes about the scripts I've provided:

  • I have a whole bunch of functions that do small jobs. The nice thing about this separation is that if you want to change how something is done (ex how you store the price of a trail), you have a single place to change
  • Though the script works, there are plenty of TODOs left. If you don't have someone who can help you with it, you'll have to do a bunch of reading (and probably asking). Try to find scripts other people have made and see how they do things -- I know other scripts exist that work with Datastores, for example.
  • I'm not sure what you wanted to have happen when the user purchases multiple trails, so I did this:
    • If the player doesn't own the trail, they purchase it and then immediately use it
    • The player can only use 1 trail at a time. If they attempt to use 2nd one, the 1st one is disabled and then shortly destroyed (giving a really cool effect if you're moving while changing them!)
    • If the player attempts to use the same trail they already have, it is cleared instead.
1
Oh, i thought it was the other way around, like the first script is local and second script is server. Well anyways, a client cannot make attachments in the first place. hiimgoodpack 2009 — 6y
0
Could you take the linked model and fix it and re upload and link it here or no? PolyyDev 214 — 6y
0
I have updated my answer! chess123mate 5873 — 6y
Ad
Log in to vote
0
Answered by
juel90 26
6 years ago
Edited 6 years ago

Few things, Cant do OnClientEvent in a Server script you firing to the client from a local script and in the server script you need a player as your first argument too

Answer this question