Set string value to player's name

  1. Hi, I currently have a remote event that clones a random model from ServerStorage and positions it above a part in the workspace labeled ‘CarSpawn’. My intent is to check if the players name matches the player who cloned the model then delete this model when the player collides with the part ‘destroyCar’. It returns “StringValue,” not the players name, and doesn’t delete the model.

There are no errors.

-- Server Script

local items = game.ServerStorage:GetChildren()
local randomItem = items[math.random(1, #items)]
local destroyCar = game.Workspace.destroyCar
local remoteEvent = game.ReplicatedStorage.SpawnCar
local playerValue = randomItem.StringValue

local function spawnCar(player)
	carClone = randomItem:Clone()
	carClone.Parent = workspace
	carClone.PrimaryPart.CFrame = game.Workspace.CarSpawn.CFrame + Vector3.new(0,5,0)
	playerValue.Value = player.Name
	-- print(playerValue)
end

local function deleteCar(player)
	if playerValue.Value == player.Name then
		carClone:Destroy()
	end
end

destroyCar.Touched:Connect(deleteCar)
remoteEvent.OnServerEvent:Connect(spawnCar)

Thank you.

1 Like

.Touched first parameter is the Instance which touched the part, to get the player use :GetPlayerFromCharacter():

local Players = game:GetService("Players")

local DestroyCar = workspace.DestroyCar

DestroyCar.Touched:Connect(function(hit)
	if hit and hit.Parent:FindFirstChildOfClass("Humanoid") then
		local Client = Players:GetPlayerFromCharacter(hit.Parent)
	end
end)
1 Like

This returns the players name on part touch, however it doesn’t set the string value within the model to the player’s name.

Try using player.UserId instead.

That’s bc the comment where it says print(PlayerValue) is printing the property name, therefore you need to do print(PlayerValue.Value)

1 Like

To my knowledge this remedied the string-return issue, however, it’s still not destroying the model.

I was able to discern the issue, here’s the solution (it was the concoction of your answers, thank you all):

-- Server Script

local items = game.ServerStorage:GetChildren()
local randomItem = items[math.random(1, #items)]
local destroyCar = game.Workspace.destroyCar
local remoteEvent = game.ReplicatedStorage.SpawnCar
local playerValue = randomItem.StringValue
local Players = game:GetService("Players")
local DestroyCar = workspace.destroyCar

local function spawnCar(player)
	carClone = randomItem:Clone()
	carClone.Parent = workspace
	carClone.PrimaryPart.CFrame = game.Workspace.CarSpawn.CFrame + Vector3.new(0,5,0)
	playerValue.Value = player.Name
	-- print(playerValue.Value)
end

DestroyCar.Touched:Connect(function(hit)
	if hit and hit.Parent:FindFirstChildOfClass("Humanoid") then
		local Client = Players:GetPlayerFromCharacter(hit.Parent)
		if playerValue.Value == Client.Name then
		carClone:Destroy()
		end
	end
end)

remoteEvent.OnServerEvent:Connect(spawnCar)
1 Like