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!