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

Giver script doesn't work? can anybody explain when a model has been touched by a person? [SOLVED]

Asked by 5 years ago
Edited 5 years ago
--Script
game.Players.PlayerAdded:Connect(function(player)
    script.Parent.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") then
            local leadstats = player:WaitForChild("leaderstats")
            local coinss = leadstats:WaitForChild("Coins")
            local coins = coinss.Value
            coins = coins + game.Lighting.cakes.Value
        end
    end)
end)

This giver will return your cakes to coins. everytime you earn a cake, the cakes.Value in Lighting will increase one by one and if you touch the giver, it will return you. BUt this script won't give it for some reason.

Can anybody explain the touch code easily and tell me why doesn't this work?

PS>I used

game.Players.PlayerAdded:Connect(function(player))

because i couldn't get LocalPlayer and leaderstats

0
You dont need to detect player added to use player, you can use :GetPlayerFromCharacter(char) (i posted a answer using this.) yHasteeD 1819 — 5y

3 answers

Log in to vote
0
Answered by
tacotown2 119
5 years ago
    game.Players.PlayerAdded:Connect(function(player)
        script.Parent.Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                local leadstats = player:WaitForChild("leaderstats")
                local coinss = leadstats:WaitForChild("Coins")
                local coins = coinss
                coins.Value = coins.Value + game.Lighting.cakes.Value
            end
        end)
    end)

u cant do value in a variable this should work

0
Thanks! It worked. Do you like tacos by any chance? ComLBY_Dev 21 — 5y
0
they are delicious tacotown2 119 — 5y
Ad
Log in to vote
0
Answered by
BenSBk 781 Moderation Voter
5 years ago

When you assign a variable to a value in Lua, you assign that variable directly to that value. You do not point to that value, or save a path to that value. Consider the following code, for example:

t = {}
t.foo = "bar"
x = t.foo -- Assign x to t.foo.
print(x) --> bar
t.foo = "baz" -- Update t.foo.
print(x) --> bar (x is not updated.)
x = t.foo -- Update x to t.foo's new value.
print(x) --> baz (x is updated.)

Likewise, assigning x to a different value would not change the value of t.foo:

x = "hello"
print(t.foo) --> baz

Your issue is that you are directly updating the coins variable, which is in no way related or linked with coinss, on line 08. To the program, it is simply some arbitrary piece of data. Instead, you should update coinss.Value directly.

Just a few small nitpicks, you should be using ServiceProvider:GetService to get all services except, optionally, Workspace.

Here's the updated code:

local cakes = game:GetService("Lighting").cakes

game:GetService("Players").PlayerAdded:Connect(function(player)
    script.Parent.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") then
            local leadstats = player:WaitForChild("leaderstats")
            local coinss = leadstats:WaitForChild("Coins")
            coinss.Value = coinss.Value + cakes.Value
        end
    end)
end)

I hope that this helped!

0
just noticed that somebody else has answered your question, but it's probably worth taking a read of this anyway to understand your problem in more detail BenSBk 781 — 5y
0
i did ComLBY_Dev 21 — 5y
Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
5 years ago
Edited 5 years ago

Problem

Do not put .Value in variables to change value's, names and more.

Example:

-- Incorrect
local value = workspace.IntValue.Value --< Default value: 1
value = value + 1
-- Value of object = 1, why? wen you change a value from variable this only change in script, not in value.

-- Correct
local value = workspace.IntValue --< Default value: 1
value.Value = value.Value + 1
-- Value of object = 2, this worked but you changed it in object, not in variable.

Correction

You dont need to detect player added, you can get character with Touch event using Players:GetPlayerFromCharacter(char)

What Players:GetPlayerFromCharacter(char) make?

  • GetPlayerFromCharacter returns the Player associated with the given Instance, or nil if one cannot be found.

Example:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then -- Check for humanoid
        local plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- Get player
        if plr then -- Check player
            print("Got player!")    
        end
    end
end)

Also for fix your problem do not use .Value in variables for change a value. You can simple remove the .Value from variable and put coins.Value = coins.Value + ...

Fixed script:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- Get player
        if player then -- Check for player
            local leadstats = player:WaitForChild("leaderstats")
            local coins = leadstats:WaitForChild("Coins")
            coins.Value = coins.Value + game.Lighting.cakes.Value
            --< Not is good way to storage items in Lighting.
        end
    end
end)

Hope it helped :)

Wiki pages

GetPlayerFromCharacter


Solved your problems? put in title [SOLVED] or accept a answer.
0
thanks ComLBY_Dev 21 — 5y

Answer this question