Teleporter wont give tool according to StringValue on leaderstats

(Sorry this is a repost! No response since 33 minutes…)

My idea was a teleporter that has a StringValue, and if it is something like “Default” as the value, it will then give the Default tool. However, it doesn’t seem to work for some reason…

local NormTP = game.Workspace.lobby.NAT
local leaderboard = game.Players
local s = math.random(1, 2)

-- balls
local Ball = game.ServerStorage.Default --> Default Ball <--
local LightningBall = game.ServerStorage.Lightning

NormTP.Touched:Connect(function(player)
	local leaderstat = player:WaitForChild("leaderstats")
	if leaderstat.Ball.Value == "Default" then
		local CloneBall = Ball:Clone()
		CloneBall.Parent = player.Backpack
		CloneBall.Enabler.Enabled = true
	else
		if leaderstat.Ball.Value == "Lightning" then
			local LightBall = LightningBall:Clone()
			LightBall.Parent = player.BackPack
			LightBall.Enabler.Enabled = true
		end
	end
end)

NormTP.Touched:Connect(function(player)
	if s == 1 then
		player.CFrame = CFrame.new(0.7, 9.8, -8.3)
	else
		if s == 2 then
			player.CFrame = CFrame.new(0.7, 9.8, 41.2)
		end
	end
end)

Are you sure that the leaderstats folder is loading correctly? All data, including the Ball value is there?

If so, is the Ball value always “Default” or “Lightning”? If it’s not always either of these, it’s not going to do anything.

Also, there’s a slight capitalization error here. player.BackPack needs to be player.Backpack.

1 Like

I can send a screenshot, and the ball value changes whenever you click on a certain part.
image

Also adding onto this. You didn’t need to fire a Touched Event twice when you could just go at the bottom and run the rest of the other event there.

Send a screenshot of the value on the leaderstats.

And is the Ball’s value ALWAYS “Default” or “Lightning”?

And you can check using prints in the script. Example:

	if leaderstat.Ball.Value == "Default" then
        print("Ball is Default")
		local CloneBall = Ball:Clone()
		CloneBall.Parent = player.Backpack
		CloneBall.Enabler.Enabled = true
	else
		if leaderstat.Ball.Value == "Lightning" then
            print("Ball is Lightning")
			local LightBall = LightningBall:Clone()
			LightBall.Parent = player.BackPack
			LightBall.Enabler.Enabled = true
		end
	end

The ball’s value at the beginning is always Default, but, if you click a certain part, it changes it to Lightning.

Send a screenshot of the values.

Any errors or warnings in the output relating to the script?

image
image
image
image

No, not any singe error inside the output nor warning

OHHH wait. I see the problem here.

.Touched’s first parameter returns the instance it hit, not the player. To get the player, you would have to do something like this:

NormTP.Touched:Connect(function(hit)
    local char = hit:FindFirstAncestorWhichIsA("Model")
    local player = game.Players:GetPlayerFromCharacter(char)

    if player then
        -- Code here
    end
end)
1 Like

OHHHH Your right I didn’t even think of that.

It worked! I just have to add debounce which is pretty easy, but it doesn’t teleport me for some reason.

Yeah, you’re trying to teleport the player, and not the character. Let me cook up an improved script:

local players = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")

local normTP = workspace:WaitForChild("lobby"):WaitForChild("NAT")

local balls = {
	Default = serverStorage:WaitForChild("Default"),
	Lightning = serverStorage:WaitForChild("Lightning")
}
local randomPositions = {
	CFrame.new(0.7, 9.8, -8.3),
	CFrame.new(0.7, 9.8, 41.2)
}

local function normTPTouched(hit)
	local char = hit:FindFirstAncestorWhichIsA("Model")
	local player = players:GetPlayerFromCharacter(char)
	
	if player then
		local random = math.random(1, 2)
		
		local leaderstats = player:WaitForChild("leaderstats")
		local ballValue = leaderstats:WaitForChild("Ball")
		
		local newBall = balls[ballValue.Value]:Clone()
		newBall.Parent = player.Backpack
		newBall:WaitForChild("Enabler").Enabled = true
		
		char:PivotTo(randomPositions[random])
	end
end

normTP.Touched:Connect(normTPTouched)

Hopefully, you can understand what’s happening in the script, lol. If not you should try to learn it, it’s some useful stuff.

1 Like

Workspace.lobby.NAT.Script:25: attempt to index nil with 'Clone'
Default ball worked, not the Lightning ball.

My bad I misspelled Lightning.

I edited the script, copy paste it again.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.