This is a regular script inside of a weapon, in a box i'm using as a weapon giver. The script works correctly when on a desktop, it gives you the "Pistol". The script also works correctly on touch devices when using "Play Here" on studio. On Local server testing or live i still am receiving the regular "Pistol", and not the desired "MobilePistol". Ive checked through the code and everything seems fine, any ideas? Thanks in advance!
local debounce = false
local inputservice = game:GetService("UserInputService")
local gun = game:GetService("ReplicatedStorage").VBuckWeapons.Common.Pistol -- Location of gun
local mobgun = game:GetService("ReplicatedStorage").VBuckWeapons.Common.MobilePistol -- Location of gun
local debouncetime = 2 -- Time of debounce
function onTouch(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local plr = game:GetService("Players"):FindFirstChild(hit.Parent.Name)
local char = plr.Character
if inputservice.KeyboardEnabled == false then
debounce = true
print("You are playing in android")
if char:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(mobgun.Name) or char:FindFirstChild(mobgun.Name) then
print("You already have a gun!")
wait(debouncetime)
debounce = false
else
mobgun:Clone().Parent = plr.Backpack
wait(debouncetime)
debounce = false
end
else
debounce = true
gun:Clone().Parent = plr.Backpack
print("You playing in computer")
wait(debouncetime)
debounce = false
return false;
end
elseif hit.Parent.Parent:FindFirstChild("Humanoid") then
local plr = game:GetService("Players"):FindFirstChild(hit.Parent.Parent.Name)
local char = plr.Character
if inputservice.KeyboardEnabled == false then
debounce = true
print("You are playing in android")
if char:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(gun.Name) or plr.Backpack:FindFirstChild(mobgun.Name) or char:FindFirstChild(mobgun.Name) then
print("You already have a gun!")
wait(debouncetime)
debounce = false
else
mobgun:Clone().Parent = plr.Backpack
wait(debouncetime)
debounce = false
end
else
debounce = true
gun:Clone().Parent = plr.Backpack
print("You playing in computer")
wait(debouncetime)
debounce = false
return false;
end
else
return false;
end
end
script.Parent.Parent.Touched:connect(onTouch)
EDIT
-- Variables --
local debounce = false
local debouncetime = 2
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(c)
local v = Instance.new("BoolValue", c)
v.Name = "Touched"
v.Value = false
end)
end)
function touch(hit)
if hit.Parent:FindFirstChild("Humanoid") and debounce == false and hit.Parent:FindFirstChild("Touched") then
debounce = true
local char = hit.Parent
char.Touched.Value = true
wait(debouncetime)
char.Touched.Value = false
debounce = false
else
return end
end
script.Parent.Touched:Connect(touch)

3 comments
i went through and cleaned the code up some, doesnt make a difference as far as effect though
You want to make on touch in a part give a gun?
I want it to give a weapon based on whether the player is mobile or not