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

Weird attempt to index nil value?

Asked by 4 years ago
Edited by theking48989987 4 years ago

hi, im trying to get a boat to spawn from a simple gui when a player clicks a button, though the remotefunction doesnt seem to work too well as the script is casting a weird attempt to index nil value error when it doesnt try to index any value at all, all its trying to do is send a invokeserver, ive tried without the "boat" part in the invokeserver event

Output: 18:14:35.653 - Workspace.BSPP1.Script:13: attempt to index a nil value 18:14:35.654 - Stack Begin 18:14:35.654 - Script 'Players.misterviggo.PlayerGui.BoatSpawnGui.LocalScript', Line 11 18:14:35.654 - Stack End

BSel = script.Parent.BoatSelector
BFrame = script.Parent.BaseFrame
BGUI = script.Parent
ExBut = script.Parent.BaseFrame.ExitButton

ExBut.MouseButton1Click:Connect(function()
    BGUI.Enabled = false
end)

BSel.MBoat1.MouseButton1Click:Connect(function()
    game.Workspace.BSPP1.SpawnReFunction:InvokeServer("Boat")
end)

Server Script:

Spawnable = 0

Coordinates = 153.375, 71.75, 43

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChildOfClass("Humanoid") then

    end
end)

script.Parent.SpawnReFunction.OnServerInvoke = function(Boat)
        if Spawnable == 0 then
        local BReplica = game.ReplicatedStorage:FindFirstChild(Boat):Clone()
        BReplica.Name = Boat
        BReplica.Parent = game.Workspace
        BReplica:SetPrimaryPartCFrame(CFrame.new(Coordinates))
    end
end

0
You don't know what you are talking about, you are attempting to index a nil value. You probably don't know how but you're doing it. Lua wouldn't lie for no reason. The issue is happening on the server script, make sure to send that as well programmerHere 371 — 4y
0
no the error guides me here but ill comment it misterviggo 61 — 4y
0
ill just send it as a answer as this didnt turn out too well misterviggo 61 — 4y

2 answers

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

It's hard to tell which line your program is erroring on, since the lines you posted don't match up with what their actual lines in game are.

I'm pretty sure it's erroring because in your function that catches the remote event, you're forgetting that the player is the first parameter since it is implicitly passed. Your boat is the second parameter, not the first, but you're naming the player parameter 'Boat'. The player doesn't exist in the replicated storage so the game will error.


Spawnable = 0 --EDIT: this needs to be a cframe. i THINK lua will ignore the rest of the variables leaving --coordinates to be only 153.375 (either that or itll error) --so made sure to wrap this in a cframe.new() so it does what you want it to do Coordinates = CFrame.new(153.375, 71.75, 43) script.Parent.Touched:Connect(function(hit) if hit.Parent:FindFirstChildOfClass("Humanoid") then --player has touched the script.Parent end end) --EDIT: I changed how the function is ran to be :Connect(function() instead of assigning the function to the OnServerInvoke -- This is where your issue probably is. --The first parameter here should be the player that invoked it, not the boat itself since --the player is implicitly passed script.Parent.SpawnReFunction.OnServerInvoke:Connect(function(playerWhoCalledEvent, Boat) if Spawnable == 0 then local BReplica = game.ReplicatedStorage:FindFirstChild(Boat):Clone() BReplica.Name = Boat BReplica.Parent = game.Workspace BReplica:SetPrimaryPartCFrame(Coordinates) end end

In case that wasn't the issue, I added some waitForChild() functions. The script might be running before the everything else inside the object was loaded. Look at the comments that say TODO: and make sure to do what it says there in case you're still having the error.

boatGui = script.Parent
boatSelector = boatGui:WaitForChild("BoatSelector")
baseFrame = boatGui:WaitForChild("BaseFrame")
exitButton = baseFrame:WaitForChild("ExitButton")

exitButton.MouseButton1Click:Connect(function()
    boatGui.Enabled = false
end)

--TODO: Check if this path exists vvv
boatSelector.MBoat1.MouseButton1Click:Connect(function()
    --TODO: Put your remote function inside the ReplicatedStorage
    --It is currently confusing since you're invoking the server with an object in the server
    --Check if this path exists vvv
    game.Workspace.BSPP1.SpawnReFunction:InvokeServer("Boat")
end)

Ad
Log in to vote
0
Answered by 4 years ago
Spawnable = 0

Coordinates = 153.375, 71.75, 43

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChildOfClass("Humanoid") then

    end
end)

script.Parent.SpawnReFunction.OnServerInvoke = function(Boat)
        if Spawnable == 0 then
        local BReplica = game.ReplicatedStorage:FindFirstChild(Boat):Clone()
        BReplica.Name = Boat
        BReplica.Parent = game.Workspace
        BReplica:SetPrimaryPartCFrame(CFrame.new(Coordinates))
    end
end

0
Look at line 13 you're attempting to index something that wasn't found programmerHere 371 — 4y
0
but the error comes to the local script and it doesnt throw an error on this one misterviggo 61 — 4y
0
and it isnt a nil value as its in this case MBoat1 through the local script misterviggo 61 — 4y
0
if boat doesn't exist the line 13 will error even if it's not erroring right now you should have a check. if you know for a fact that it's not going to error, use game.ReplicatedStorage[Boat]:Clone() royaltoe 5144 — 4y

Answer this question