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

I can't fix this?!?

Asked by 9 years ago

So its a tycoon. and this is the collector brick that removes the block and gives you money

``script.Parent.Touched:connect(function(hit)
    local N = script.Parent.Parent.Parent.Owner.Value
    local P = game.Players:GetPlayerFromCharacter(N)
     if P:FindFirstChild("leaderstats") ~= nil then
    local Mon = P.leaderstats.Cash.Value
        if hit.Name == "Tril" then
            Mon = Mon + 10
        elseif hit.Name == "TriU" then
            Mon = Mon + 20
        end
    else
        print("Leaderstats is not there.")

    end
end)
``

but it keeps giving me this error: Unable to cast value to Object

and i have a StringValue 3Parents up that contains the players name when he buys the tycoon.

1 answer

Log in to vote
1
Answered by
dyler3 1510 Moderation Voter
9 years ago

Try this:

script.Parent.Touched:connect(function(hit)
    local N = script.Parent.Parent.Parent.Owner.Value
    local P = game.Players:FindFirstChild(N) --Changed this line
    if P:FindFirstChild("leaderstats") ~= nil then
        local Mon = P.leaderstats.Cash.Value
        if hit.Name == "Tril" then
            Mon = Mon + 10
        elseif hit.Name == "TriU" then
            Mon = Mon + 20
        end
    else
        print("Leaderstats is not there.")
    end
end)

The problem was that you were trying to define the player as a string, which you can't do. You need to have the script search through the players with the string, and connect it to the Players name. Anyways, I believe this should work now. If you have any further problems or questions, please leave a comment below. Hope I helped :P

0
Bro i am so dumb XD i should have tried this before Facepalm CoffeeMesh 5 — 9y
1
xD, not a big deal. I make simple mistakes like this all the time. Just make sure to look over your script carefully and make sure everything makes sense how it is. Glad I could help though :P dyler3 1510 — 9y
Ad

Answer this question