Issue with a scoring system

Hello! I’m trying to make a scoring system for a basketball game. It works as follows: The script detects the last player with the ball, and if that player scores and it touches a part inside the hoop, the player is given 100 of our in-game currency. Here is the script, it is located in ServerScriptService:

local DataStoreService = game:GetService("DataStoreService")
local NerosDataStore = DataStoreService:GetDataStore("PlayerNeros")

local lastPlayerWithBall = nil

local function updateLeaderstats(player, score)
	-- Find the player's leaderstats
	local leaderstats = player:FindFirstChild("leaderstats")
	if not leaderstats then
		return
	end

	-- Find the Neros value
	local neros = leaderstats:FindFirstChild("Neros")
	if not neros then
		return
	end

	-- Update the Neros value
	neros.Value = score

	-- Save the updated Neros value to the DataStore
	local success, errorMessage = pcall(function()
		NerosDataStore:SetAsync(tostring(player.UserId), score)
	end)
	if not success then
		warn("Failed to save Neros for player " .. player.Name .. ": " .. errorMessage)
	end
end

game.Workspace.Ball.Touched:Connect(function(hit)
	if hit.Name == "Touched" then
		-- Player has scored, perform scoring logic here
		local player = game.Players:GetPlayerByUserId(lastPlayerWithBall)
		if player then
			print(player.Name .. " has scored!")

			-- Update and save leaderstats
			local score = (player.leaderstats.Neros.Value or 0) + 100
			updateLeaderstats(player, score)
		end
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	-- Assign the player's UserId to the lastPlayerWithBall variable when they touch the ball
	player.CharacterAdded:Connect(function(character)
		local ball = character:WaitForChild("Ball")
		if ball then
			lastPlayerWithBall = player.UserId
		end
	end)

	-- Reset the lastPlayerWithBall variable when the player leaves the game
	player.CharacterRemoving:Connect(function(character)
		lastPlayerWithBall = nil
	end)
end)

The issue here is that the line print(player.Name .. " has scored!") is not outputting when a player scores, so the entire script is not working. Can anybody find the problem here? Thanks for helping!

4 Likes

Is the part’s name in the hoop “Touched”? Try adding a print statement that just prints out hit in the .Touched event.

4 Likes

Alright, here are some statements I added:

game.Workspace.Ball.Touched:Connect(function(hit)
	print("hit something")
	if hit.Name == "Touched" then
		print("hit part")

However, none of these seem to output.

3 Likes

How does the ball move? Is its movement done on the client? If so, the movement of the ball is not being registered by the server.

Try walking into the ball and seeing if the event fires.

3 Likes

When I touch the ball, it prints out “hit something”.

2 Likes

Does the ball move on the client or the server? If you switch to server mode when testing, are you able to see the ball move?

2 Likes

The ball is visible on the server.

2 Likes

Is the ball visually touching the Touched part on the server?

1 Like

No, the “Touched” part is invisible.
Here is a picture:

1 Like

Sorry, I meant if you can see the ball passing through the hoop on the server.

Also, is the CanTouch property enabled?

1 Like

Nevermind, I didn’t look at the properties tab.

CanCollide is a property relating to physics that allows a part to go through it, whereas CanTouch is a property for scripting, and it allows a .Touched event to fire. CanQuery is also a property for scripting, and it allows it to be detected as a “hit” object. Turning CanQuery on should solve your issue.

2 Likes

Thanks! I enabled CanTouch and it works fine.

2 Likes

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