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

Why am I getting this error when I try and call my function?

Asked by 4 years ago
Edited 4 years ago

So I am simply creating a function and calling the function whenever a certain dummy's uppertorso,upperleftarm, and upperrightarm are touched. However, I keep getting this error:

"ServerScriptService.Rounds.Round1:33: attempt to index function with 'UpperTorso'"

Not sure what the issue is if anyone can solve it and explain the problem that would be great!

local IR = game.Workspace.GameData.InRound
local RoundNumber = workspace.GameData.RoundNumber
local R1 = game.ReplicatedStorage.Events.Rounds.Round1
--Variables for each customer
local C1 = game.workspace.C1
local humanoid = C1.Humanoid


local pointA = game.Workspace.GreenFlag
local pointB = game.Workspace.PurpleFlag
local pointC = game.Workspace.YellowFlag


--functions for each customer
--Customer1
function C1(hit)
    C1 = game.workspace.C1
    local humanoid = C1.Humanoid
    if hit.Parent.Name == "Pizza" then
        local Players = game:GetService("Players")  
        hit.Parent:Destroy()
        --Gives Player Money
        for i, player in pairs(Players:GetPlayers()) do
            local cash = player.leaderstats.Cash
            cash.Value = cash.Value +10
        end
        humanoid:MoveTo(pointB.PrimaryPart.Position)
        humanoid.MoveToFinished:Wait()
        humanoid:MoveTo(pointC.PrimaryPart.Position)
    end
end

C1.UpperTorso.Touched:Connect(C1)
C1.LeftUpperArm.Touched:Connect(C1)
C1.RightUpperArm.Touched:Connect(C1)

1 answer

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

The problem comes from the fact that you defined C1 as game.Workspace.C1

BUT then you used that same variable in your function by naming your function C1

local IR = game.Workspace.GameData.InRound
local RoundNumber = workspace.GameData.RoundNumber
local R1 = game.ReplicatedStorage.Events.Rounds.Round1
--Variables for each customer
local C1 = game.workspace.C1
local humanoid = C1.Humanoid


local pointA = game.Workspace.GreenFlag
local pointB = game.Workspace.PurpleFlag
local pointC = game.Workspace.YellowFlag


--functions for each customer
--Customer1
function C1Function(hit) -- changed function name for you. Can't use a variable you are already using. Use something else.
    C1 = game.workspace.C1
    local humanoid = C1.Humanoid
    if hit.Parent.Name == "Pizza" then
        local Players = game:GetService("Players")  
        hit.Parent:Destroy()
        --Gives Player Money
        for i, player in pairs(Players:GetPlayers()) do
            local cash = player.leaderstats.Cash
            cash.Value = cash.Value +10
        end
        humanoid:MoveTo(pointB.PrimaryPart.Position)
        humanoid.MoveToFinished:Wait()
        humanoid:MoveTo(pointC.PrimaryPart.Position)
    end
end

C1.UpperTorso.Touched:Connect(C1Function)-- changed all of your connection lines
C1.LeftUpperArm.Touched:Connect(C1Function)
C1.RightUpperArm.Touched:Connect(C1Function)

This should have fixed it for you.

A function name counts as a variable. That's why I always go with connection lines in the same line that I make the function, avoiding names. But sometimes they are useful.

But in this example, I think you did alright since there are multiple events.

0
Thanks, I also realized that I'm a dummy. TooMuchRice 10 — 4y
0
no worries. sometimes you just run into problems in programming. I do all the time lol. when you do, just take a break and come back to it...or you can always ask questions here. legoguy939 418 — 4y
Ad

Answer this question