Train delivery System

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to achieve a system that gives the player money once their train hits a brick on the rail

  2. What is the issue?
    The issue is that the script is not working (no errors or anything in output).

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried using remote events, I have also tried to use this method but none worked. I looked on the Developer Forum however there was no answer related and I tried a few but they didn’t work.

function touched(hit, player)
	local engine = hit.Parent.Parent.Parent.Engine
	local owner = engine.Owner.Value
	if hit.Parent.Name == (owner.Name .. ' train') then
		local leaderstats = owner.Parent:WaitForChild("leaderstats")
		if leaderstats then
			leaderstats.Cash.Value = leaderstats.Cash.Value + 100 
		end
	end
end
script.Parent.Touched:Connect(touched)
1 Like

Checking thr values exist first. And Log each part touched

1 Like

The values are valid and the train doesn’t give an error when touched.

1 Like
function touched(hit, player)
	local engine = hit.Parent.Parent.Parent.Engine
if engine then --
	local owner = engine.Owner.Value
	if hit.Parent.Name == (owner .. ' train') then
		local leaderstats = owner.Parent:WaitForChild("leaderstats")
		if leaderstats then
			leaderstats.Cash.Value += 100
		 end
	  end
   end
end
script.Parent.Touched:Connect(touched)
1 Like

Also ‘owner’ variable seems Value.
Why using owner.Name?

local owner = engine.Owner.Value

if hit.Parent.Name == (owner.Name .. ' train') then
1 Like

Is it possible that owner is a Character rather than a Player instance?

A good way to debug what’s wrong is adding a few print statements throughout this code, within every if statement.

The reason you are getting no errors but nothing is happening is because this code is stopping somewhere along the way. Whether hit.parent.Name isn’t actually (owner.Name .. ' train') or if there is no “leaderstats” under owner.Parent.

Add a few prints to see where your code is stopping!

1 Like

Maybe, ‘owner’ is ObjectValue.

1 Like

It’s the value of an ObjectValue - just seeing what it’s actually set as!

1 Like

If owner is Player instance, why finding leaderstats in Players?

local owner = engine.Owner.Value
local leaderstats = owner.Parent:WaitForChild("leaderstats")
2 Likes

Isn’t that where leaderstats are set? Or I’m I wrong?

Edit: nvm I just saw parent. I’m kinda tired.

2 Likes

Given you’re using owner.Value, owner must be a value/variable instance. You need to get player from the players class. If Owner.Value is the playerId, then you could just do:

local owner = Players[engine.Owner.Value]

(as long as you define Players correctly)

2 Likes

First of all, have you check that the ‘if leaderstats then’ is ever getting reached because I find when I’m programming that I always miss something here and there which means that the statement is never reached. Obviously, you could simply check with a print statement.

Yes, owner is a character. However i don’t see the problem.

Because i need to get the character’s name

I will test this when i get home, I’m on my phone so i can’t help much.

So, Is owner value is player instance? or character instance?

What is in Object Value?
image

player.Character is the object value

Maybe

owner.Parent

will be Workspace

Maybe this work! try it

local db = true -- making debounce
local TouchCoolTime = 2 -- make cool time
script.Parent.Touched:Connect(function(hit)
	local engine = hit.Parent.Parent.Parent.Engine
	
	if engine and db then 
		db = false
		local owner = engine.Owner.Value -- Character Instance
		local player = game.Players:GetPlayerFromCharacter(owner) -- Player Instance
		
		if hit.Parent.Name == (owner.Name .. ' train') then
			local leaderstats = player.leaderstats
			leaderstats.Cash.Value += 100
		end
	end
	wait(TouchCoolTime) 
		db = true
end)