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

Why does the Value not change?

Asked by 3 years ago

I'm scripting a lawn-mowing game and I've come across a problem. So every time I cut the grass(trawa) the IntValue goes up by 1. It works fine when it is assigned to a pick-up object and the points go straight to Player's Inventory folder:

local trawa = script.Parent

local function collect(otherPart)
    local player = game.Players:FindFirstChild(otherPart.Parent.Name)
    if player then
        local trawaType = trawa.Name
        local trawaStat = player.Inventory:FindFirstChild(trawaType)
        if trawaStat then
            trawa:Destroy()
            trawaStat.Value = trawaStat.Value + 1
        end
    end
end

trawa.Touched:Connect(collect)

So that script works fine, but that's not the way I want it to work. I want the points to be assigned directly after cutting grass, not after I collect a pick-up it drops. So I changed the script above so that the points are assigned to the tool's inventory and then are copied to player's inventory.

local trawa = script.Parent

local function onTouch(otherPart)
    local tool = otherPart.Parent
    if tool:IsA('Tool') and tool.Kosa.Value == true then
        trawa.Parent = nil     
        local trawaType = trawa.Name
        local trawaStat = tool.Inventory:FindFirstChild(trawaType)
        trawaStat.Value = trawaStat.Value + 1
        print(trawaStat.Value)
        wait(3)
        trawa.Parent = workspace
    end
end

trawa.Touched:Connect(onTouch)

The value does indeed change because it goes up when I print it, but it's not changing in the properties and is not registered by other scripts, because it's not copied to player's inventory. Then this is the script in ServerScriptService that should transfer points from the tool to player:

local starter = game:GetService("StarterPack")
local tool = starter:FindFirstChildWhichIsA('Tool')
local player = game:GetService("Players").LocalPlayer


function points(player)
    player.Inventory.Trawa2.Value = player.Inventory.Trawa2.Value + tool.Inventory.Trawa2.Value
    tool.Inventory.Trawa2.Value = 0


end

tool.Inventory.Trawa2.Changed:Connect(points)

Changing IntValue to NumberValue doesn't solve the problem.

0
Do you get any errors in the output? Have you checked that the value yours trying to change is the correct one? epicnmac 63 — 3y
0
No, there are no errors. Cymusichan 7 — 3y

2 answers

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

you cant get a player from a server script , local player = game.Players.LocalPlayer will return "nil" , this scripts is full of errors ,I recommand you to start a new one and well oraganise it ,go learn about remote events and server/client boundary too, cuz for example if you insert a new part to workspace using a local script , the player that own the local script will see the part but the server and the other players wont see it so same thing for values and everything else.

0
You're right thanks, but localPlayer appears only in the last script and my problem is somewhere within the second one. Cymusichan 7 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Solved the problem myself and it works even better than I expected. The points go straight to the player's inventory. I found out that you can use GetPlayerFromCharacter, because the tool is parented to the character when equipped.

local trawa = script.Parent
local players = game:GetService("Players")

local function onTouch(otherPart)
    local tool = otherPart.Parent
    local player = players:GetPlayerFromCharacter(tool.Parent)
    if tool:IsA('Tool') and tool.Kosa.Value == true then
        trawa.Parent = nil  
        local trawaType = trawa.Name
        local trawaStat = player.Inventory:FindFirstChild(trawaType)
        if trawaStat then
            trawaStat.Value = trawaStat.Value + 1
        end
        print(trawaStat.Value)

        wait(3)
        trawa.Parent = workspace

    end
end

trawa.Touched:Connect(onTouch)

Answer this question