--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
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
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!
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.
You dont need to detect player added, you can get character with Touch event using Players:GetPlayerFromCharacter(char)
What Players:GetPlayerFromCharacter(char)
make?
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 :)