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

Cloning a hat to the character with Filtering Enabled?

Asked by
RoyMer 301 Moderation Voter
8 years ago

Cloning a Hat from the ServerStorage into the Character with Filtering Enabled.

When I click the button, the StringValue's value becomes the name of the frame and then through the Filtering Enabled, I'm trying to read the name of the value located in the client and search it in the ServerStorage and cloning it in the character.

This is the output I'm getting: ServerScriptService.Filtering Enabled:26: attempt to index a nil value

Part of the script in the Client

local hat = player.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Hat")
hat.Value = script.Parent.Parent.Name
game.Workspace.FE.Hat:InvokeServer()

In the Server Script

function FE.Hat.OnServerInvoke(player)
    local Hat = player.PlayerGui.ScreenGui.Hat
    game.ServerStorage:FindFirstChild(Hat.Value):Clone().Parent = player.Character
end
0
Which line here coresponds to line 26 in your 'Filtering Enabled' script under ServerScriptService? BlackJPI 2658 — 8y
0
Line 3 of the Server Script: game.ServerStorage:FindFirstChild(Hat.Value):Clone().Parent = player.Character RoyMer 301 — 8y
0
It is likely that the returned value from FindFirstChild is nil BlackJPI 2658 — 8y

1 answer

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

You could try passing the Hat value through the invocation as an argument.

Client:

local hat = player.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Hat")
hat.Value = script.Parent.Parent.Name
game.Workspace.FE.Hat:InvokeServer(hat.Value)

Server:

function FE.Hat.OnServerInvoke(player, hat)
    local Hat = game.ServerStorage:FindFirstChild(hat)
    if Hat then
        Hat:Clone().Parent = player.Character
    end
end

I don't know which line is line 26 so I don't know if that was the problem. If not, please elaborate. Hope this helped.

Explanation Edit: On the client, hat.Value is passed to the invocation as whatever the value type is (in this case, a string). The server doesn't even know there's a ValueObject involved-- all it receives is the value.

Let's say hat.Value == "Roblohunk". On line 3 of the LocalScript, before even invoking the server, the script gets the value of hat.Value-- "Roblohunk"-- and then passes it to the server. So, if hat.Value == "Roblohunk", line 3 is basically:

game.Workspace.FE.Hat:InvokeServer("Roblohunk")

This then passes to the OnServerInvoke function which, as you know, automatically takes the player who invoked as a parameter. That means the argument we passed is the second one, which I specified as 'hat'.

Considering the previous example where hat.Value == "Roblohunk", here's an explanation of the function:

function FE.Hat.OnServerInvoke(player, hat)
    local Hat = game.ServerStorage:FindFirstChild(hat) -- Find the first child named "Roblohunk" (or whatever hat.Value is) in ServerStorage. If a match isn't found, :FindFirstChild() returns nil. So the 'Hat' variable either becomes the hat, or nil.
    if Hat then -- We check to see if the hat exists (if it found "Roblohunk" in ServerStorage).
        Hat:Clone().Parent = player.Character -- If it does, we clone it and set its parent to player.Character.
    end
end

Hope this helped to clear it up a bit.

0
Thanks you're a life saver, it worked although I don't know what you did exactly, how did the code recognize hat as hat.Value? Once again thanks a lot because it worked! RoyMer 301 — 8y
0
(by hat I mean to the argument the one in the brackets) RoyMer 301 — 8y
0
Edited. Pyrondon 2089 — 8y
0
Thanks a lot! RoyMer 301 — 8y
0
No problem. Pyrondon 2089 — 8y
Ad

Answer this question